使用缓冲字节流与文件字节流复制文件时间对比
源文件大小:224MB
结果:
通过改变文件字节流与缓冲字节流read(byte[] b) b的数组长度对比运行时间发现:
随着输入流里数组长度增加(数组长度依次从10K,20K,1M增加),程序运行时间随之增加,文件字节流运行时间普遍比缓冲字节流运行时间快2-30ms。(数组长度依次从1K,5K,8k增加)时,文件字节流与缓冲字节流运行时间长很多。当输入流读取数组长度接近缓冲字节流默认缓冲大小(8K)时,文件字节流与缓冲字节流运行时间相差1ms。
由此说明一个问题,要想提升数据读写效率,输入流一次读入字节数要小于缓冲区大小。
(我没有自定义缓冲字节流缓冲大小)可能是因为缓冲字节流默认缓冲大小为8K(太小)导致缓冲字节流的优势没有体现出来。 (题外话)在写代码时因为read方法无参导致一直读取第一个字节数据导致产生死循环,文件无限复制。兄弟们谨慎使用无参的read方法!!!
package com.day1111;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestOutputStream {
public static void main(String[] args) throws IOException {
// FileOutputStream fos = new FileOutputStream("abc.txt",false);
// byte [] b = "床前明月光,疑似地上霜。\n举头望明月,低头思故乡。".getBytes();
// fos.write(b);
// fos.close();
// copyFile("src.png","srcCopy.png");
long start = System.currentTimeMillis();
copyFile("D:\\gogo.mp4","D:\\IOCopy.mp4");
long end = System.currentTimeMillis();
System.out.println(end-start);
}
static void copyFile(String src, String dec) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(dec,false);
byte [] b = new byte[1024*1024];
int len = 0;
while((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.day1111;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestBufferedOutputStream {
public static void main(String[] args) throws IOException {
// BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Demo.txt"));
// bos.write("才是真的好".getBytes());
// bos.flush();
// bos.close();
// BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\gogo.mp4"));
// BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\BufferIOCopy.mp4"));
// int len = 0;
// byte[] b = new byte[1024*100];
// while((len = bis.read()) != -1) {
// bos.write(b, 0, len);
//
// }
// if(bos != null) {
// bos.flush();
// bos.close();
// }
// if(bis != null) {
// bis.close();
// }
long start = System.currentTimeMillis();
copyFile("D:\\gogo.mp4", "D:\\BufferIOCopy.mp4");
long end = System.currentTimeMillis();
System.out.println(end-start);
}
static void copyFile(String src, String dec) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(src));
bos = new BufferedOutputStream(new FileOutputStream(dec));
int len = 0;
byte[] b = new byte[1024*1024];
while((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
} catch (Exception e) {
// TODO: handle exception
}finally {
try {
if(bos != null) {
bos.flush();
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(bis != null) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}