java Io 缓冲流 BufferedInputStream BufferedOutputStream 笔记
2016-03-31 11:22
393 查看
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.junit.Test; /** * * Io流 * * 4个抽象基类 * * 抽象基类 节点流:-- 文件字节流缓冲流 ---处理流: 的一种 (缓冲流:文件操作的效率高。) * InputStream FileInputStream BufferedInputStream * OutputStream FileOutputStreamBufferedOutputStream 需要--循环后flush(); * 节点流:-- 文件字符流 * Reader FileReaderBufferedReader * Writer FileWriterBufferedWriter 需要--循环后flush(); * * * * 处理流 : ---- 缓冲流 * * * BufferedInputStream * BufferedOutputStream * * BufferedReader-----------------可以继续 用 char[]固定 数组长度 接收 , * 也可以用 BufferedReader.readLine() 一行行接收。 该方法返回 读取此行的字符串 * BufferedWriter ----------------- 需要--循环后flush(); * * 缓冲流的 练习 。------ 缓冲流关闭 时 close() 会自动关闭 节点流 。 不用人工手动调用。 * * * 对 word 文件 的 【 复制 】 使用 字节流, 不能使用字符流 , word中 可能会存在 图片等信息 !!!!!。 * * * * * @author Administrator * */ public class TestBufferedInputStream { /** * 1. 利用 处理流 中的 缓冲流 ---- 实现 字节文件 --音频 视频 图片 的复制。 */ @Test public void testcopy(){ long start = System.currentTimeMillis(); String src = "D:\\io\\1.wmv"; String des = "D:\\io\\1_b_copy.wmv"; this.copyZijieFile(src, des); long end = System.currentTimeMillis(); System.out.println("耗时:"+(end - start)); //144毫秒 -- 效率比 使用 节点流 中 字节流 快。 } /** * 1、利用 字节--缓冲流 复制 图片 。 */ @Test public void testBufferedInputStream(){ // 第一步: File file = new File("d:\\io\\1.jpg"); File dest = new File("d:\\io\\1_b_copy.jpg"); // 第三步 :把字节流 加入到 缓冲流中 提高 文件处理的效率 BufferedInputStream bis=null; BufferedOutputStream bos =null; try { // 第二步 FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(dest); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); byte[] b = new byte[1024]; int len; while((len = bis.read(b))!=-1){ // 缓冲流 把信息 写到磁盘 bos.write(b, 0, len); // 最后一次的数组信息从内存请移到磁盘 bos.flush(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(bis!= null){ try { bis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(bos!= null){ try { bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * 利用 -----字节缓冲流---复制 图片 视频 音频等字节文件。 * @param src * @param des */ public void copyZijieFile(String src ,String des){ // 第一步: File file = new File(src); File dest = new File(des); // 第三步 :把字节流 加入到 缓冲流中 提高 文件处理的效率 BufferedInputStream bis=null; BufferedOutputStream bos =null; try { // 第二步 FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(dest); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); byte[] b = new byte[1024]; int len; while((len = bis.read(b))!=-1){ // 缓冲流 把信息 写到磁盘 bos.write(b, 0, len); // 最后一次的数组信息从内存请移到磁盘 bos.flush(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(bis!= null){ try { bis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
相关文章推荐
- ckeditor html标签的class 等attribute属性都被屏蔽啦,替换成空的解决方案
- 对比HTML和XML
- Booktrack Classroom(有声电子书)
- div内元素是否在可见区域的判断与控制
- Booktrack Classroom(有声电子书)
- Tokonda Messenger(信使)
- 文章标题
- jQuery EasyUI使用教程之在数据网格的页脚中显示摘要信息
- jQuery仪表盘指示器动画插件 6种仪表样式
- jquery的$().each,$.each的区别
- js如何获取一个月的天数 data javascript
- 编译器重复定义错误:error C2371: 'SIZE' : redefinition; different basic types
- js 无线弹窗
- css 兼容大全
- 前端web学习记录
- Tokonda Messenger(信使)
- 用HTML+CSS实现简单的下拉列表
- javascript笔记——jsonp
- CSS左侧固定宽 右侧自适应(兼容所有浏览器)
- 18、bootStap JavaScript插件