Java第十六天(二)
对比字节输入、输出流与高效缓冲区的输入、输出流文件拷贝速度
由下面测试的结果我们可以得出结论:
| InputStream+ OutputStream+ 单个字节 | InputStream+ OutputStream+ 字节数组 | BufferedInputStream+ BufferedOutputStream+ 单个字节 | BufferedInputStream+ BufferedOutputStream+ 字节数组 |
|---|---|---|---|
| 6406毫秒 | 10毫秒 | 61毫秒 | 4毫秒 |
public class CopyFileSpeedTest {
/**
* 此次测试文件大小为2.43 MB (2,549,668 字节)
*/
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
// byteStreamCopyFile();
// arrayStreamCopyFile();
// byteBufferedStreamCopyFile();
// arrayBufferedStreamCopyFile();
long endTime = System.currentTimeMillis();
System.out.println("此方法运行时间为:"+(endTime - startTime) +"毫秒");
}
/**
* 使用字节输入、输出流单个字节拷贝文件
* 使用时间:6406毫秒
*/
public static void byteStreamCopyFile() {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream("Test.pdf");
out = new FileOutputStream("AAA.pdf");
int len = -1;
while((len = in.read()) != -1) {
out.write(len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
try {
if(out != null) {
out.close();
}
if(in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 使用字节输入、输出流与数组拷贝文件
* 使用时间:10毫秒
*/
public static void arrayStreamCopyFile() {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream("Test.pdf");
out = new FileOutputStream("AAA.pdf");
byte[] be = new byte[1024];
int len = -1;
while((len = in.read(be)) != -1) {
out.write(be,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
try {
if(out != null) {
out.close();
}
if(in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 使用字节高效缓冲区输入、输出流单个字节拷贝文件
* 使用时间:61毫秒
*/
public static void byteBufferedStreamCopyFile() {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream("Test.pdf"));
out = new BufferedOutputStream(new FileOutputStream("AAA.pdf"));
int len = -1;
while((len = in.read()) != -1) {
out.write(len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
try {
if(out != null) {
out.close();
}
if(in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 使用字节高效缓冲区输入、输出流与数组拷贝文件
* 使用时间: 4毫秒
*/
public static void arrayBufferedStreamCopyFile() {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream("Test.pdf"));
out = new BufferedOutputStream(new FileOutputStream("AAA.pdf"));
byte[] be = new byte[1024];
int len = -1;
while((len = in.read(be)) != -1) {
out.write(be,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
try {
if(out != null) {
out.close();
}
if(in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
本文对比了使用InputStream、OutputStream与使用BufferedInputStream、BufferedOutputStream进行文件拷贝的速度。测试结果显示,缓冲区的使用显著提高了文件拷贝效率。
1312

被折叠的 条评论
为什么被折叠?



