代码:
import java.io.*;
public class BufferedStream {
public static void main(String[] args) {
long start=System.currentTimeMillis();//为了看复制的时间
copy();
long end=System.currentTimeMillis();
System.out.println((end-start)+"毫秒");
}
// 通过字节流的缓冲区完成复制
public static void copy() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream("D:\\光明.mp3"));
bos = new BufferedOutputStream(new FileOutputStream("C:/光明.mp3"));
int len = 0;
while ((len = bis.read()) != -1) {
bos.write(len);
}
} catch (Exception e) {
throw new RuntimeException("复制失败!");
} finally {
try {
if (bis != null)
bis.close();
} catch (Exception e2) {
throw new RuntimeException("读取关闭失败!");
}
try {
if (bos != null)
bos.close();
} catch (Exception e2) {
throw new RuntimeException("写入关闭失败!");
}
}
}
}