53 lines
1.9 KiB
Java
53 lines
1.9 KiB
Java
package com.inmind.buffered_stream01;
|
|
|
|
import java.io.*;
|
|
|
|
public class Demo03 {
|
|
|
|
//使用字节缓冲输入输出流复制一个文件,并计算出时间(一次读写一个字节数组)
|
|
public static void main(String[] args) throws IOException {
|
|
/*FileInputStream fis = new FileInputStream("D:\\io_test\\upload\\1.jpg");
|
|
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\2.jpg");*/
|
|
|
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\io_test\\upload\\1.jpg"));
|
|
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\io_test\\upload\\3.jpg"));
|
|
|
|
byte[] bytes = new byte[1024];//读取到的字节数据保存到该数组中
|
|
int len ;//获取读取到的字节数据的长度
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
while ((len = bis.read(bytes)) != -1) {
|
|
bos.write(bytes,0,len);
|
|
}
|
|
long end = System.currentTimeMillis();
|
|
|
|
System.out.println("使用字节流复制文件花费:"+(end- start));//3ms
|
|
|
|
bis.close();
|
|
bos.close();
|
|
}
|
|
|
|
|
|
//使用原始的字节输入输出流复制一个文件,并计算出时间(一次读写一个字节数组)
|
|
public static void method(String[] args) throws IOException {
|
|
FileInputStream fis = new FileInputStream("D:\\io_test\\upload\\1.jpg");
|
|
FileOutputStream fos = new FileOutputStream("D:\\io_test\\upload\\2.jpg");
|
|
|
|
byte[] bytes = new byte[1024];//读取到的字节数据保存到该数组中
|
|
int len ;//获取读取到的字节数据的长度
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
while ((len = fis.read(bytes)) != -1) {
|
|
fos.write(bytes,0,len);
|
|
}
|
|
long end = System.currentTimeMillis();
|
|
|
|
System.out.println("使用字节流复制文件花费:"+(end- start));//8ms
|
|
|
|
fis.close();
|
|
fos.close();
|
|
}
|
|
}
|