import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 用字节缓冲流拷贝文件
*/
public class CopyTest {
public static void main(String[] args) {
// 声明字节输入流与字节输出流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 创建字节输入流与字节输出流,以原始字节流作为构造方法的参数
bis = new BufferedInputStream(new FileInputStream("a\\b.txt"));
bos = new BufferedOutputStream(new FileOutputStream("a\\c.txt"));
// 创建数组
byte[] b = new byte[10];
// 读取文件内容
int len = bis.read(b);
while (len != -1) {
// 一边读取一边写入
bos.write(b, 0, len);
// 刷新缓冲区
bos.flush();
// 接着读取
len = bis.read(b);
}
System.out.println("拷贝成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关流
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}