Recently I was learning java NIO, and then I wrote three programs to test, adding buffers to traditional IO, direct buffers, and non-direct buffers. The test results show that the direct buffer is the fastest, and the traditional buffer is the fastest. IO plus buffer second, and finally NIO non-direct buffer.
package com.wmx.test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
/**
* 通道的主要实现类 java.nio.channels.Channel FileChannel 本地的通道 网络的通道 SocketChannel
* ServerSocketChannel DatagramChannel
*
*
* @author Shinelon
*
*/
public class TestChannel {
//共耗时1017毫秒
//通道之间的数据传输
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
FileChannel inChannel=null;
FileChannel outChannel=null;
try {
inChannel = FileChannel.open(Paths.get("E:/BaiduNetdiskDownload/Microsoft Office 2013(64位).iso"),StandardOpenOption.READ);
outChannel = FileChannel.open(Paths.get("E:/BaiduNetdiskDownload/1.jpg"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);
// inChannel.transferTo(0, inChannel.size(), outChannel);
outChannel.transferFrom(inChannel, 0, inChannel.size());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(inChannel!=null){
try {
inChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outChannel!=null){
try {
outChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("共耗时"+(endTime-startTime)+"毫秒");
}
public static void main3(String[] args) {
//共耗时2728毫秒
long startTime = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
fis = new FileInputStream(new File("E:/BaiduNetdiskDownload/Microsoft Office 2013(64位).iso"));
fos = new FileOutputStream(new File("E:/BaiduNetdiskDownload/1.jpg"));
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] by = new byte[1024];
int len = 0;
while ((len = bis.read(by)) != -1) {
bos.write(by);
bos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("Total time" + (endTime - startTime) + "milliseconds");
}
/ / Use the direct buffer to complete the file copy
public static void main(String[] args) {
// The total time is 1512 milliseconds, double the total time 1093 milliseconds
long startTime = System.currentTimeMillis();
FileChannel inChannel = null;
FileChannel outChannel = null;
MappedByteBuffer inMapperChannel;
MappedByteBuffer outMapperChannel;
try {
inChannel = FileChannel.open(Paths.get("E:/BaiduNetdiskDownload/Microsoft Office 2013(64bit).iso"),
StandardOpenOption.READ);
outChannel = FileChannel.open(Paths.get("E:/BaiduNetdiskDownload/4 .jpg"), StandardOpenOption.WRITE,
StandardOpenOption.READ, StandardOpenOption.CREATE_NEW);
// Principle and original allocate
// Memory mapped file
inMapperChannel = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
outMapperChannel = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());
// Directly read and write data to the buffer
byte[] by = new byte[inMapperChannel.limit()];
inMapperChannel.get(by);
outMapperChannel.put(by);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outChannel != null) {
try {
outChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inChannel != null) {
try {
inChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("total time" + (endTime - startTime) + "milliseconds");
}
// non-direct buffer
public static void main1(String[] args ) {
// Total time 3630 ms Total time 3592 ms Total time 3751 ms
long startTime = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
// 1. Use the channel to complete the file copy
fis = new FileInputStream("E:/BaiduNetdiskDownload/Microsoft Office 2013(64bit).iso");
fos = new FileOutputStream("E:/BaiduNetdiskDownload/5.jpg");
// Get channel
inChannel = fis.getChannel();
outChannel = fos.getChannel();
// Allocate buffer of specified size
ByteBuffer buf = ByteBuffer. allocate(1024);
// store the data in the channel into the buffer
while (inChannel.read(buf) != -1) {
buf.flip();// switch to read data mode
// write data into the channel
outChannel.write(buf);
buf.clear();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outChannel != null) {
try {
outChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inChannel != null) {
try {
inChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("总耗时" + (endTime - startTime) + "毫秒");
}
}