1.测试传统的IO FileInputStream FileOutputStream
@Test
public void testIOIs() throws IOException {
File f = new File("D:\\2.exe");
if (f.exists()) {
f.delete();
}
long start = System.currentTimeMillis();
InputStream is = new FileInputStream("D:\\1.exe");
OutputStream os = new FileOutputStream(f);
byte buff[] = new byte[1024];
while (is.read(buff) != -1) {
os.write(buff);
}
long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
}
结果为:2567ms
2.测试 1中加BufferedInputStream BufferedOutputStream
@Test
public void testIOIsBuff() throws IOException {
File f = new File("D:\\2.exe");
if (f.exists()) {
f.delete();
}
long start = System.currentTimeMillis();
InputStream is = new FileInputStream("D:\\1.exe");
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream os = new FileOutputStream(f);
BufferedOutputStream fos = new BufferedOutputStream(os);
byte buff[] = new byte[1024];
while (bis.read(buff) != -1) {
fos.write(buff);
}
long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
}
结果为:833ms
3.测试 RandomAccessFile
@Test
public void testIO() throws IOException {
File f = new File("D:\\2.exe");
if (f.exists()) {
f.delete();
}
long start = System.currentTimeMillis();
RandomAccessFile fromFile = new RandomAccessFile("D:\\1.exe", "rw");
RandomAccessFile toFile = new RandomAccessFile("D:\\2.exe", "rw");
byte buff[] = new byte[1024];
while (fromFile.read(buff) != -1) {
toFile.write(buff);
}
long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
}
结果为:2634ms
4.测试 NIO 中Channel ByteBuffer
@Test
public void testNIOBuffer() throws IOException {
File f = new File("D:\\2.exe");
if (f.exists()) {
f.delete();
}
long start = System.currentTimeMillis();
FileInputStream is = new FileInputStream("D:\\1.exe");
FileChannel fci = is.getChannel();
FileOutputStream os = new FileOutputStream(f);
FileChannel fco = os.getChannel();
ByteBuffer buff = ByteBuffer.allocate(1024 * 1024 * 6);
while (fci.read(buff) != -1) {
buff.flip();
fco.write(buff);
buff.clear();
}
long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
}
结果为:776ms channel buffer 传输速度受到buffer大小的影响
5.测试 FileChannel transferTo FileInputStream FileOutputStream
@Test
public void transferTestIO() throws IOException {
File f = new File("D:\\2.exe");
if (f.exists()) {
f.delete();
}
long start = System.currentTimeMillis();
FileInputStream fromFile = new FileInputStream("D:\\1.exe");
FileOutputStream toFile = new FileOutputStream(f);
FileChannel fromChannel = fromFile.getChannel();
FileChannel toChannel = toFile.getChannel();
long position = 0;
long count = fromChannel.size();
fromChannel.transferTo(position, count, toChannel);
long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
}
测试结果为:430ms
- 6.测试 FileChannel transferTo RandomAccessFile
@Test
public void transferTest() throws IOException {
File f = new File("D:\\2.exe");
if (f.exists()) {
f.delete();
}
long start = System.currentTimeMillis();
RandomAccessFile fromFile = new RandomAccessFile("D:\\1.exe", "rw");
FileChannel fromChannel = fromFile.getChannel();
RandomAccessFile toFile = new RandomAccessFile("D:\\2.exe", "rw");
FileChannel toChannel = toFile.getChannel();
long position = 0;
long count = fromChannel.size();
fromChannel.transferTo(position, count, toChannel);
long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
}
结果为:398ms
7.最后总结一下 可以看到 最后这种管道数据转移方式最快