// 4)使用缓冲流加数组,一次读一个数组量的数据
private static void m4() throws IOException {
InputStream in = new FileInputStream("源文件路径");
OutputStream out = new FileOutputStream("目标文件路径");
BufferedInputStream bin = new BufferedInputStream(in);
BufferedOutputStream bout = new BufferedOutputStream(out);
long t1 = System.currentTimeMillis();
int i = -1;
byte[] b = new byte[1024];
while ((i = bin.read(b)) != -1) {
bout.write(b, 0, i);
bout.flush();
}
long t2 = System.currentTimeMillis();
System.out.println("第四种方法复制共用时" + (t2 - t1) + "秒");
bout.close();
bin.close();
}
// 3)使用缓冲流,不使用数组
private static void m3() throws IOException {
FileInputStream in = new FileInputStream("源文件路径");
FileOutputStream out = new FileOutputStream("目标文路径");
BufferedOutputStream bout = new BufferedOutputStream(out);
BufferedInputStream bin = new BufferedInputStream(in);
int i = -1;
long t1 = System.currentTimeMillis();
while ((i = bin.read()) != -1) {
bout.write(i);
bout.flush();
}
long t2 = System.currentTimeMillis();
System.out.println("第三种方法复制共用时" + (t2 - t1) + "秒");
// 第一种方法复制共用时372880秒
bout.close();
bin.close();
}
// 1)使用字节流,一次读一个字节
private static void m1() throws IOException {
FileInputStream in = new FileInputStream("源文件路径");
FileOutputStream out = new FileOutputStream("目标文路径");
int i = -1;
long t1 = System.currentTimeMillis();
while ((i = in.read()) != -1) {
out.write(i);
out.flush();
}
long t2 = System.currentTimeMillis();
System.out.println("第一种方法复制共用时" + (t2 - t1) + "秒");
// 第一种方法复制共用时372880秒
out.close();
in.close();
}
// 2)使用字节流加数组,一次读一个数组量的数据
private static void m2() throws IOException {
FileInputStream in = new FileInputStream("源文件路径");
FileOutputStream out = new FileOutputStream("目标文路径");
long t1 = System.currentTimeMillis();
int i = -1;
byte[] b = new byte[1024];
while ((i = in.read(b)) != -1) {
out.write(b, 0, i);
out.flush();
}
long t2 = System.currentTimeMillis();
System.out.println("第二种方法复制共用时" + (t2 - t1) + "秒");
out.close();
in.close();
}
} 作者: 劉芒 时间: 2016-8-22 22:24
请多多指教!!!作者: 悠悠呦呦 时间: 2016-8-22 22:27
还没学到 谢谢分享作者: luliang 时间: 2016-8-22 23:23
很不错..可以看出来用了多少时间