把文件abc.txt复制一份名为demo.txt的文件
package cn.zzx.file;
import java.io.*;
public class BufferedDemo {
public static void main(String[] args) throws Exception {
File f1 = new File("D:/demo.txt"); // 写入数据
File f2 = new File("D:/abc.txt"); //读取数据
InputStream read = new FileInputStream(f2);
OutputStream write = new FileOutputStream(f1);
//放到缓存区
BufferedInputStream br = new BufferedInputStream(read);
BufferedOutputStream bw = new BufferedOutputStream(write);
byte [] b = new byte[1024];
int len = 0;
//循环读取,读完返回负1
while((len=br.read(b))!=-1){
//边读边写
bw.write(b, 0,len);
}
bw.flush();//强制刷新缓冲区
bw.close(); //关闭写出流
br.close();//关闭读入流
}
}