主要對nio的讀效率進行對比,代碼以下:app
public class NIOTest {dom
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// nioReadTest1();
// nioReadTest2();
// nioReadTest3();
// nioReadTest4();
nioReadTest5();
// nioReadTest6();
}ip
private static void nioReadTest1() throws Exception {
FileInputStream in = new FileInputStream(new File("data"));
FileChannel channel = in.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
int readCount = -1;
long totalCount = 0;
long start = System.currentTimeMillis();
while ((readCount = channel.read(buf)) != -1) {
totalCount += readCount;
buf.flip();
// hand data
buf.clear();get
}
long end = System.currentTimeMillis();
System.out.println("讀取:" + totalCount + "個字節,耗時:" + (end - start));
channel.close();
}io
private static void nioReadTest2() throws Exception {
RandomAccessFile raf = new RandomAccessFile(new File("data"), "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = ByteBuffer.allocate(1024);
int readCount = -1;
long totalCount = 0;
long start = System.currentTimeMillis();
while ((readCount = channel.read(buf)) != -1) {
totalCount += readCount;
buf.flip();
// hand data
buf.clear();class
}
long end = System.currentTimeMillis();
System.out.println("讀取:" + totalCount + "個字節,耗時:" + (end - start));
channel.close();
}效率
private static void nioReadTest3() throws Exception {
FileChannel channel = FileChannel.open(Paths.get("data"),
StandardOpenOption.READ);
ByteBuffer buf = ByteBuffer.allocate(1024);
int readCount = -1;
long totalCount = 0;
long start = System.currentTimeMillis();
while ((readCount = channel.read(buf)) != -1) {
totalCount += readCount;
buf.flip();
// hand data
buf.clear();map
}
long end = System.currentTimeMillis();
System.out.println("讀取:" + totalCount + "個字節,耗時:" + (end - start));
channel.close();
}channel
private static void nioReadTest4() throws Exception {
SeekableByteChannel channel = Files.newByteChannel(Paths.get("data"),
StandardOpenOption.READ);
ByteBuffer buf = ByteBuffer.allocate(1024);
int readCount = -1;
long totalCount = 0;
long start = System.currentTimeMillis();
while ((readCount = channel.read(buf)) != -1) {
totalCount += readCount;
buf.flip();
// hand data
buf.clear();nio
}
long end = System.currentTimeMillis();
System.out.println("讀取:" + totalCount + "個字節,耗時:" + (end - start));
channel.close();
}
private static void nioReadTest5() throws Exception {
FileChannel channel = FileChannel.open(Paths.get("data"),
StandardOpenOption.READ);
ByteBuffer buf = ByteBuffer.allocateDirect(1024*1024*1024);
int readCount = -1;
long totalCount = 0;
long start = System.currentTimeMillis();
while ((readCount = channel.read(buf)) != -1) {
totalCount += readCount;
buf.flip();
// hand data
buf.clear();
}
long end = System.currentTimeMillis();
System.out.println("讀取:" + totalCount + "個字節,耗時:" + (end - start));
channel.close();
}
private static void nioReadTest6() throws Exception {
FileChannel channel = FileChannel.open(Paths.get("data"),
StandardOpenOption.READ);
MappedByteBuffer mapBuf = channel.map(MapMode.READ_ONLY, 0,
channel.size());
mapBuf.load();
System.out.println(mapBuf.isLoaded());
byte[] data = new byte[1024];
int lop = 1024 * 1024;
long start = System.currentTimeMillis();
for(int i=0;i<lop;i++){
mapBuf.get(data);
}
long end = System.currentTimeMillis();
System.out.println("耗時:" + (end - start));
channel.close();
}
} 對比結果:FileInputStream 、RandomAccessFile 、FileChannel.open、Files.newByteChannel四種打開FileChannel的方式、讀取效率差很少;普通的ByteBuffer與DirectByteBuffer其實差異不是很大,可是DirectByteBuffer會快點;MappedByteBuffer就要快不少了,通常對於大文件建議採用這種方式