四种文件复制方式的效率比较
- 文件复制方式,字节流,一共4个方式,源文件的大小是:40.8 MB (42,885,472 字节)
- 1.字节流读写单个字节 运行时间:210546ms
- 2.字节流读写字节数组 运行时间:438ms
- 3.字节流缓冲区读写单个字节 运行时间:2304ms
- 4.字节流缓冲区读写字节数组 运行时间:137ms
public static void main(String[] args) {
long start=System.currentTimeMillis();
File src = new File("E:\\software\\kugou8322.exe");
File desc = new File("d:\\oracle code\\longzhoufeng\\kugou.exe");
copy_1(src, desc);
long end=System.currentTimeMillis();
System.out.println(end-start);
}
方法4:实现文件复制
/**
* 方法4:实现文件复制
* 1.字节流缓冲区读写字节数组
* 运行时间:137ms
*/
public static void copy_4(File src, File desc) {
try {
BufferedInputStream bufis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0;
byte[] bt=new byte[1024];
while ((len = bufis.read(bt)) != -1) {
bufos.write(bt,0,len);
}
bufis.close();
bufos.close();
} catch (IOException ex) {
System.out.println(ex);
throw new RuntimeException("文件写入失败");
}
}
方法3:实现文件复制
/**
* 方法3:实现文件复制
* 1.字节流缓冲区读写单个字节
* 运行时间:2304ms
*/
public static void copy_3(File src, File desc) {
try {
BufferedInputStream bufis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(desc));
int len = 0;
while ((len = bufis.read()) != -1) {
bufos.write(len);
}
bufis.close();
bufos.close();
} catch (IOException ex) {
System.out.println(ex);
throw new RuntimeException("文件写入失败");
}
}
方法2:实现文件复制
/**
* 方法2:实现文件复制
* 1.字节流读写字节数组
* 运行时间:438ms
*/
public static void copy_2(File src, File desc) {
try {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0;
byte[] bt = new byte[1024];
while ((len = fis.read(bt)) != -1) {
fos.write(bt,0,len);
}
fis.close();
fos.close();
} catch (IOException ex) {
System.out.println(ex);
throw new RuntimeException("文件写入失败");
}
}
方法1:实现文件复制
/**
* 方法1:实现文件复制
* 1.字节流读写单个字节
* 运行时间:210546ms
*/
public static void copy_1(File src, File desc) {
try {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(desc);
int len = 0;
while ((len = fis.read()) != -1) {
fos.write(len);
}
fis.close();
fos.close();
} catch (IOException ex) {
System.out.println(ex);
throw new RuntimeException("文件写入失败");
}
}