package demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopyTest {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
// 单字节
func1();
long func1Time = System.currentTimeMillis();
System.out.print("func1:");
System.out.println(func1Time - startTime);
// 单字节,BUFFER缓冲
func2();
long func2Time = System.currentTimeMillis();
System.out.print("func2:");
System.out.println(func2Time - func1Time);
// 字节数组
func3();
long func3Time = System.currentTimeMillis();
System.out.print("func3:");
System.out.println(func3Time - func2Time);
// 字节数组,BUFFER缓冲
func4();
long func4Time = System.currentTimeMillis();
System.out.print("func4:");
System.out.println(func4Time - func3Time);
}
public static void func1() {
File file1 = null;
File file2 = null;
FileInputStream in = null;
FileOutputStream out = null;
try {
file1 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL.jpg");
file2 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL-copy1.jpg");
in = new FileInputStream(file1);
out = new FileOutputStream(file2);
int n = -1;
while ((n = in.read()) != -1)
out.write(n);
} catch (IOException e) {
System.out.println(e.toString());
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
System.out.println(e);
}
if (out != null)
try {
out.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
public static void func2() {
File file1 = null;
File file2 = null;
BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
file1 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL.jpg");
file2 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL-copy2.jpg");
in = new BufferedInputStream(new FileInputStream(file1));
out = new BufferedOutputStream(new FileOutputStream(file2));
int n = -1;
while ((n = in.read()) != -1)
out.write(n);
} catch (IOException e) {
System.out.println(e.toString());
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
System.out.println(e);
}
if (out != null)
try {
out.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
public static void func3() {
File file1 = null;
File file2 = null;
FileInputStream in = null;
FileOutputStream out = null;
byte[] b = new byte[1024];
int n = -1;
try {
file1 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL.jpg");
file2 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL-copy3.jpg");
in = new FileInputStream(file1);
out = new FileOutputStream(file2);
while ((n = in.read(b)) != -1)
out.write(b, 0, n);
} catch (IOException e) {
System.out.println(e.toString());
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
System.out.println(e);
}
if (out != null)
try {
out.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
public static void func4() {
File file1 = null;
File file2 = null;
BufferedInputStream in = null;
BufferedOutputStream out = null;
byte[] b = new byte[1024];
int n = -1;
try {
file1 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL.jpg");
file2 = new File("C:\\Users\\yhd82\\Desktop\\YHDJL-copy4.jpg");
in = new BufferedInputStream(new FileInputStream(file1));
out = new BufferedOutputStream(new FileOutputStream(file2));
while ((n = in.read(b)) != -1)
out.write(b, 0, n);
} catch (IOException e) {
System.out.println(e.toString());
} finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
System.out.println(e);
}
if (out != null)
try {
out.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
}
/*
* 测试结果:
* 文件大小:9.57 MB (10,040,209 字节)
* func1:130261 ms
* func2:480 ms
* func3:169 ms
* func4:44 ms
*
*/
IO流文件复制缓冲效率测试
最新推荐文章于 2019-08-17 19:16:00 发布