java Io 缓冲流 BufferedInputStream BufferedOutputStream 笔记

来源:互联网 发布:北洋时期 知乎 编辑:程序博客网 时间:2024/04/28 12:35
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. 利用   处理流     中的  缓冲流    ---- 实现      字节文件 --音频  视频   图片    的复制。*/@Testpublic  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、利用 字节--缓冲流                     复制   图片 。  */@Testpublic  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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(bis!= null){try {bis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(bos!= null){try {bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(bis!= null){try {bis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}

0 0
原创粉丝点击