将一个txt文件,复制到另一个txt文件中(缓冲字节流(BufferedInputStream,BufferedOutputStream))

2020-10-23 10:32:20 浏览数 (1)

1.代码

代码语言:javascript复制
package d04_test;/*
 * zt
 * 2020/8/8
 * 15:07
 *使用缓冲字节流实现复制文件(BufferedInputStream)
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        //1.创建缓冲字节流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("e:\aaa.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:\bbb.txt"));
        //边读边写
        byte[] buf = new byte[1024];
        int len = 0;
        while((len=bis.read(buf))!=-1){
            bos.write(buf,0,len);
            bos.flush();
        }
        bis.close();
        bos.close();
        System.out.println("复制好了");
    }
}

运行结果

代码语言:javascript复制
复制好了
txt

0 人点赞