Java IO和Java NIO在文件拷貝上的性能差異分析
1. 在JAVA傳統的IO系統中,讀取磁盤文件數據的過程如下:
以FileInputStream類為例,該類有一個read(byte b[])方法,byte b[]是我們要存儲讀取
到用戶空間的緩沖區。參看read(byte b[])方法的源碼,可知,它會在內部再調用readBytes(b, 0, b.length)方法,而且readBytes(b, 0, b.length)方法是一個native方法(即本地方法),最終通過這個本地方法來發起一次系統調用,即調用系統內核的read()方法,內核從磁盤讀取數據到內核緩沖區,這個過程由磁盤控制器通過DMA操作將數據從磁盤讀取取內核緩沖區,此過程不依賴于CPU。然后用戶進程再將數據從內核緩沖區拷貝到用戶空間緩沖區。用戶進程再從用戶空間緩沖區中讀取數據。因為用戶進程是不可以直接訪問硬件的。所以需要通過內核來充當中間人的作用來實現文件的讀取。整個過程如下圖所示:
2. 自從JAVA 1.4以后,JAVA在NIO在引入了文件通道的概念,在API中有提供了一個
FileChannel類。該類與傳統的IO流進行關聯。可以由FileInputStream或FileOutputStream獲取該文件通道,我們可以通過通道對文件進行讀寫操作。
3.JAVA NIO中還引入了文件內存映射的概念:現代操作系統大都支持虛擬內存映射,這樣,我們可以把內核空間地址與用戶空間的虛擬地址映射到同一個物理地址,這樣,DMA 硬件(只能訪問物理內存地址)就可以填充對內核與用戶空間進程同時可見的緩沖區了。如下圖所示:
這樣做的好處是,我們在讀取磁盤文件時,再也不用通過內核緩沖區到用戶進程緩沖區的來回拷貝操作了。操作系統會通過一些頁面調度算法來將磁盤文件載入對分頁區進行高速緩存的物理內存。我們就可以通過映射后物理內存來讀取磁盤文件了。
3. 下面我們通過三種不同方式文件拷貝的案例來驗證文件通道及文件內存映射在IO
系統中的作用。測試環境為windows 32位系統和JDK1.6。代碼中使用的測試文件movie.avi為一個123MB的視頻文件。代碼如下:
package cn.com.hbust.nio.file;import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class FileCopyTest { public static void main(String[] args) throws Exception { String sourcePath = "F:\\mywork\\javademo\\dir1\\movie.avi"; String destPath1 = "F:\\mywork\\javademo\\dir2\\movie1.avi"; String destPath2 = "F:\\mywork\\javademo\\dir2\\movie2.avi"; String destPath3 = "F:\\mywork\\javademo\\dir2\\movie3.avi"; long t1 = System.currentTimeMillis(); traditionalCopy(sourcePath,destPath1); long t2 = System.currentTimeMillis(); System.out.println("傳統IO方法實現文件拷貝耗時:" + (t2-t1) + "ms"); nioCopy(sourcePath,destPath2); long t3 = System.currentTimeMillis(); System.out.println("利用NIO文件通道方法實現文件拷貝耗時:" + (t3-t2) + "ms"); nioCopy2(sourcePath,destPath3); long t4 = System.currentTimeMillis(); System.out.println("利用NIO文件內存映射及文件通道實現文件拷貝耗時:" + (t4-t3) + "ms"); } private static void nioCopy2(String sourcePath, String destPath) throws Exception { File source = new File(sourcePath); File dest = new File(destPath); if(!dest.exists()) { dest.createNewFile(); } FileInputStream fis = new FileInputStream(source); FileOutputStream fos = new FileOutputStream(dest); FileChannel sourceCh = fis.getChannel(); FileChannel destCh = fos.getChannel(); MappedByteBuffer mbb = sourceCh.map(FileChannel.MapMode.READ_ONLY, 0, sourceCh.size()); destCh.write(mbb); sourceCh.close(); destCh.close(); } private static void traditionalCopy(String sourcePath, String destPath) throws Exception{ File source = new File(sourcePath); File dest = new File(destPath); if(!dest.exists()) { dest.createNewFile(); } FileInputStream fis = new FileInputStream(source); FileOutputStream fos = new FileOutputStream(dest); byte [] buf = newbyte [512]; int len = 0; while((len = fis.read(buf)) != -1) { fos.write(buf, 0, len); } fis.close(); fos.close(); } private static void nioCopy(String sourcePath, String destPath) throws Exception{ File source = new File(sourcePath); File dest = new File(destPath); if(!dest.exists()) { dest.createNewFile(); } FileInputStream fis = new FileInputStream(source); FileOutputStream fos = new FileOutputStream(dest); FileChannel sourceCh = fis.getChannel(); FileChannel destCh = fos.getChannel(); destCh.transferFrom(sourceCh, 0, sourceCh.size()); sourceCh.close(); destCh.close(); } } </pre> <p></p>
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!相關經驗
相關資訊