java輸入輸出專題--第二部分
主要對nio的讀效率進行對比,程式碼如下:
public class NIOTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// nioReadTest1();
// nioReadTest2();
// nioReadTest3();
// nioReadTest4();
nioReadTest5();
// nioReadTest6();
}
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();
}
long end = System.currentTimeMillis();
System.out.println("讀取:" + totalCount + "個位元組,耗時:" + (end - start));
channel.close();
}
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();
}
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();
}
long end = System.currentTimeMillis();
System.out.println("讀取:" + totalCount + "個位元組,耗時:" + (end - start));
channel.close();
}
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();
}
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就要快很多了,一般對於大檔案建議採用這種方式