码迷,mamicode.com
首页 > 编程语言 > 详细

Java InputStream、String、File相互转化 --- good

时间:2016-11-25 20:52:34      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:int   write   --   append   tostring   close   buffer   span   http   

 

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 InputStream、String、File相互转化 --- good

标签:int   write   --   append   tostring   close   buffer   span   http   

原文地址:http://www.cnblogs.com/softidea/p/6102720.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!