Java InputStreamStringFile相互转化 --- good

Posted 沧海一滴

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java InputStreamStringFile相互转化 --- good相关的知识,希望对你有一定的参考价值。

 

String --> InputStream
ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());

InputStream --> String
String inputStream2String(InputStream is){
   BufferedReader in = new BufferedReader(new InputStreamReader(is));
   StringBuffer buffer = new StringBuffer();
   String line = "";
   while ((line = in.readLine()) != null){
     buffer.append(line);
   }
   return buffer.toString();
}


File --> InputStream

InputStream in = new FileInputStream(file);

 

InputStream --> File

public void inputstreamtofile(InputStream ins,File file){
   OutputStream os = new FileOutputStream(file);
   int bytesRead = 0;
   byte[] buffer = new byte[8192];
   while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
      os.write(buffer, 0, bytesRead);
   }
   os.close();
   ins.close();
}

 

http://blog.sina.com.cn/s/blog_a000da9d010121bl.html

 

以上是关于Java InputStreamStringFile相互转化 --- good的主要内容,如果未能解决你的问题,请参考以下文章

Java 布尔运算

java [Java] Java常用代码#java

Java - 35 Java 实例

Java While 循环

Java 字符串

Java If ... Else