NIO与IO进行文件读取耗时对比,最近由于项目上用到IO操作,传统IO性能不佳

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013050790/article/details/72851634

代码如下:

复制代码
  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 }
复制代码


测试结果:

1 采用传统IO FileInputStream 读取,耗时:11393
2 采用传统IO BufferedInputStream 读取,耗时:1191
3 采用传统IO RandomAccessFile 读取,耗时:2929
4 采用NIO FileChannel 自带方法  读取,耗时:485
5 采用NIO FileChannel 循环 读取,耗时:2502

猜你喜欢

转载自blog.csdn.net/u013050790/article/details/72851634