1. 程式人生 > >NIO與IO進行檔案讀取耗時對比,最近由於專案上用到IO操作,傳統IO效能不佳

NIO與IO進行檔案讀取耗時對比,最近由於專案上用到IO操作,傳統IO效能不佳

複製程式碼
  1 package my;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.BufferedReader;
  6 import java.io.BufferedWriter;
  7 import java.io.File;
  8 import java.io.FileInputStream;
  9 import java.io.FileOutputStream;
 10 import java.io.FileReader;
11 import java.io.FileWriter; 12 import java.io.RandomAccessFile; 13 import java.io.Reader; 14 import java.io.Writer; 15 import java.nio.ByteBuffer; 16 import java.nio.channels.FileChannel; 17 18 /** 19 * 檔案拷貝各種方式的比較 20 * 21 */ 22 public class TestFile { 23 24 public final static
String FILE_PATH ="D:\\電影\\[電影天堂www.dy2018.com]特殊身份BD中英雙字.mkv"; 25 26 //public final static String FILE_PATH = "F:\\apache-tomcat-7.0.11\\webapps\\ROOT\\a.rar"; 27 28 public final static String FILE_PATH_OUT = "D:\\哈哈.mkv"; 29 30 public static void TransByCommonIoStream() throws Exception {
31 32 long beginTime = System.currentTimeMillis(); 33 34 FileInputStream fis = new FileInputStream(new File(FILE_PATH)); 35 36 FileOutputStream fos = new FileOutputStream(new File(FILE_PATH_OUT)); 37 38 byte[] b = new byte[1024]; 39 40 int len = 0; 41 42 while ((len = fis.read(b)) != -1) { 43 fos.write(b, 0, len); 44 } 45 46 fos.flush(); 47 48 fis.close(); 49 fos.close(); 50 51 long endTime = System.currentTimeMillis(); 52 53 System.out.println("採用傳統IO FileInputStream 讀取,耗時:" 54 + (endTime - beginTime)); 55 56 } 57 58 public static void TransByCommonIoBufferedStream() throws Exception { 59 60 long beginTime = System.currentTimeMillis(); 61 62 FileInputStream fis = new FileInputStream(new File(FILE_PATH)); 63 64 FileOutputStream fos = new FileOutputStream(new File(FILE_PATH_OUT)); 65 66 BufferedInputStream bis = new BufferedInputStream(fis); 67 68 BufferedOutputStream bos = new BufferedOutputStream(fos); 69 70 byte[] b = new byte[1024]; 71 72 int len = 0; 73 74 while ((len = bis.read(b)) != -1) { 75 bos.write(b, 0, len); 76 } 77 78 bos.flush(); 79 80 fis.close(); 81 fos.close(); 82 bis.close(); 83 bos.close(); 84 85 long endTime = System.currentTimeMillis(); 86 87 System.out.println("採用傳統IO BufferedInputStream 讀取,耗時:" 88 + (endTime - beginTime)); 89 90 } 91 92 public static void TransByCommonIoBuffered() throws Exception { 93 94 long beginTime = System.currentTimeMillis(); 95 96 Reader br = new BufferedReader(new FileReader(new File(FILE_PATH))); 97 Writer bw = new BufferedWriter(new FileWriter(new File(FILE_PATH_OUT))); 98 99 char[] c = new char[1024]; 100 101 int len = 0; 102 103 while ((len = br.read(c)) != -1) { 104 bw.write(c, 0, len); 105 } 106 107 bw.flush(); 108 br.close(); 109 bw.close(); 110 111 long endTime = System.currentTimeMillis(); 112 113 System.out.println("採用傳統IO BufferedReader 讀取,耗時:" 114 + (endTime - beginTime)); 115 } 116 117 public static void TransByRandomAccFile() throws Exception { 118 119 long beginTime = System.currentTimeMillis(); 120 121 FileInputStream fis = new FileInputStream(new File(FILE_PATH)); 122 123 RandomAccessFile raf = new RandomAccessFile(new File(FILE_PATH_OUT), 124 "rw"); 125 126 byte[] b = new byte[1024]; 127 128 int len = 0; 129 130 while ((len = fis.read(b)) != -1) { 131 raf.write(b, 0, len); 132 } 133 134 long endTime = System.currentTimeMillis(); 135 136 System.out.println("採用傳統IO RandomAccessFile 讀取,耗時:" 137 + (endTime - beginTime)); 138 139 } 140 141 /** 142 * 採用FileChannel 自帶方法測試 public abstract long 143 * transferFrom(ReadableByteChannel src, long position, long count) throws 144 * IOException; 145 */ 146 public static void TransByNioFileChannel() throws Exception { 147 148 long beginTime = System.currentTimeMillis(); 149 150 FileChannel fc = new FileInputStream(new File(FILE_PATH)).getChannel(); 151 152 // FileChannel fco = new RandomAccessFile(new File(FILE_PATH_OUT), "rw").getChannel(); 153 FileChannel fco = new FileOutputStream(new File(FILE_PATH_OUT)).getChannel(); 154 155 fco.transferFrom(fc, 0, fc.size()); 156 157 long endTime = System.currentTimeMillis(); 158 159 System.out.println("採用NIO FileChannel 自帶方法 讀取,耗時:" 160 + (endTime - beginTime)); 161 } 162 163 public static void TransByNioFileChannelCommon() throws Exception { 164 165 long beginTime = System.currentTimeMillis(); 166 167 FileChannel fc = new FileInputStream(new File(FILE_PATH)).getChannel(); 168 169 FileChannel fco = new RandomAccessFile(new File(FILE_PATH_OUT), "rw") 170 .getChannel(); 171 172 ByteBuffer buf = ByteBuffer.allocate(1024); 173 174 while (fc.read(buf) != -1) { 175 buf.flip(); 176 fco.write(buf); 177 buf.clear(); 178 } 179 180 long endTime = System.currentTimeMillis(); 181 182 System.out.println("採用NIO FileChannel 迴圈 讀取,耗時:" 183 + (endTime - beginTime)); 184 } 185 186 public static void deleteFile() { 187 File f = new File(FILE_PATH_OUT); 188 if (f.exists()) 189 f.delete(); 190 } 191 192 public static void main(String[] args) throws Exception { 193 194 TransByCommonIoStream(); 195 deleteFile(); 196 TransByCommonIoBufferedStream(); 197 deleteFile(); 198 TransByRandomAccFile(); 199 deleteFile(); 200 TransByNioFileChannel(); 201 deleteFile(); 202 TransByNioFileChannelCommon(); 203 deleteFile(); 204 } 205 }
複製程式碼