Java InputStream、String、File相互转化
2016-05-02 16:43
501 查看
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(); }
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- android上改变listView的选中颜色
- String.intern
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- sql2005 附加数据库出错(错误号:5123)解决方法
- PostgreSQL ERROR: invalid escape string 解决办法
- C#中File类的文件操作方法详解
- 浅谈C++中的string 类型占几个字节
- 标准C++类string的Copy-On-Write技术
- C++实现string存取二进制数据的方法
- C#中string和StingBuilder内存中的区别实例分析
- 详解C++中实现继承string类的MyString类的步骤
- asp #include file 与 #include virtual 的区别小结第1/2页
- PHP STRING 陷阱原理说明
- PHP file_exists问题杂谈
- c#中 String和string的区别介绍
- C#实现的图片、string相互转换类分享