java中InputStream String
2014-10-09 14:57
162 查看
Java 中获取输入流时,有时候需要将输入流转成String,以便获取其中的内容 ,下面总结一下 InputStream 转成String 的方式
方法1:
public String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
方法2:
public String inputStream2String (InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
方法3:
public static String inputStream2String(InputStream is) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i=-1;
while((i=is.read())!=-1){
baos.write(i);
}
return baos.toString();
}
String 转成 InputStream
String str = "String与InputStream相互转换";
InputStream in_nocode = new ByteArrayInputStream(str.getBytes());
InputStream in_withcode = new ByteArrayInputStream(str.getBytes("UTF-8"));
方法1:
public String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
方法2:
public String inputStream2String (InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
方法3:
public static String inputStream2String(InputStream is) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i=-1;
while((i=is.read())!=-1){
baos.write(i);
}
return baos.toString();
}
String 转成 InputStream
String str = "String与InputStream相互转换";
InputStream in_nocode = new ByteArrayInputStream(str.getBytes());
InputStream in_withcode = new ByteArrayInputStream(str.getBytes("UTF-8"));
相关文章推荐
- Java中String和InputStream的转换
- java中byte,String,InputStream之间的转换
- java中byte,String,InputStream之间的转换 .
- Java 里把 InputStream 转换成 String 的几种方法
- java中byte,String,InputStream之间的转换
- java里面InputStream类型转换成String类型怎么实现?
- String与InputStream相互转换----------Java学习
- java将InputStream转为String
- java中byte []、String、inputstream之间的转换
- Java 里把 InputStream 转换成 String 的几种方法
- In Java, how do I read/convert an InputStream to a String? - Stack Overflow
- Java InputStream、String、File相互转化
- Java 里把 InputStream 转换成 String 的几种方法
- Java 里把 InputStream 转换成 String 的几种方法
- java中byte,String,InputStream之间的转换
- java中byte,String,InputStream之间的转换
- java byte[] inputstream string 转换
- Java 里把 InputStream 转换成 String 的几种方法
- Java 里把 InputStream 转换成 String 的几种方法