IO流(字节流复制)01

package ioDemo;

import java.io.*;

/**
 * IO流(字节流复制)
 * Created by lcj on 2017/11/2.
 */
public class bufferReadWrintDemo {
    //声明异常
    public static void main(String[] args) throws IOException {
//        copy01();
        copy02();
//        copy03();
//        copy04();
    }
    //自定义缓冲区
    public static  void copy01() throws IOException
    {
        FileInputStream fileInputStream = new FileInputStream("D:\\Project\\IdeaProjects\\test01_time\\src\\main\\java\\ioDemo\\2.txt");
        FileOutputStream  fileOutputStream= new FileOutputStream("D:\\Project\\IdeaProjects\\test01_time\\src\\main\\java\\ioDemo\\1.txt");
        //声明数组大小
        byte[] buf01 = new byte[1024];
        int len01 = 0 ;
        while ((len01 = fileInputStream.read(buf01) )!= -1)
        {
            fileOutputStream.write(buf01,0,len01);//将buf01中数据写入至len01中
        }
    }
    //自定义缓冲区方法BufferedInputStream
    public static  void copy02() throws  IOException
    {
        FileInputStream fileInputStream = new FileInputStream("D:\\Project\\IdeaProjects\\test01_time\\src\\main\\java\\ioDemo\\2.txt");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

        FileOutputStream  fileOutputStream= new FileOutputStream("D:\\Project\\IdeaProjects\\test01_time\\src\\main\\java\\ioDemo\\22.txt");
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        byte[] buf02 = new byte[1024];
        int len02 = 0 ;
        while ((len02 = bufferedInputStream.read(buf02)) != -1)
        {
            bufferedOutputStream.write(buf02,0,len02);
            bufferedOutputStream.flush();  //刷新缓冲区数据
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }
    //不建议使用此方法,此方法按照字节读取,效率低
    public static void copy03() throws IOException
    {
        FileInputStream fileInputStream = new FileInputStream("D:\\Project\\IdeaProjects\\test01_time\\src\\main\\java\\ioDemo\\2.txt");
        FileOutputStream  fileOutputStream= new FileOutputStream("D:\\Project\\IdeaProjects\\test01_time\\src\\main\\java\\ioDemo\\3.txt");

        int len03 = 0;
        while ((len03 = fileInputStream.read()) != -1)
        {
            fileOutputStream.write(len03);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }
    ///
    public static  void copy04()  throws IOException
    {
        FileInputStream fileInputStream = new FileInputStream("D:\\Project\\IdeaProjects\\test01_time\\src\\main\\java\\ioDemo\\2.txt");
        FileOutputStream  fileOutputStream= new FileOutputStream("D:\\Project\\IdeaProjects\\test01_time\\src\\main\\java\\ioDemo\\4.txt");

        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = fileInputStream.read(buf)) != -1)
        {
            fileOutputStream.write(buf, 0, len);
//            System.out.println(new String(buf ,0 ,len));
        }
        fileOutputStream.close();
        fileInputStream.close();
    }

}

 

posted on 2017-11-02 16:42  lcj122  阅读(117)  评论(0编辑  收藏  举报

导航