In this example, FileChannel and BufferedInputStream are used for test and comparison.
TestHandler. java is used to implement dynamic proxy and test the running efficiency.
Package com. test; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; public class TestHandler implements InvocationHandler {private Object obj = null; public TestHandler (Object obj) {this. obj = obj;} public static Object newInstance (Object obj) {Object result = Proxy. newProxyInstance (obj. getClass (). getClassLoader (), obj. getClass (). getInterfaces (), new TestHandler (obj); return result ;}@ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {Object result = null; system. out. println ("executing... "); long start = System. currentTimeMillis (); result = method. invoke (obj, args); long end = System. currentTimeMillis (); System. out. println ("execution completed, time consumed:" + (end-start); return result ;}}
INIOTest. java
package com.test;import java.io.IOException;public interface INIOTest{public void copyFileMapped(String oldPath,String newPath) throws IOException;public void copyFileNIO(String oldPath,String newPath) throws IOException;public void copyFile(String oldPath,String newPath) throws IOException;}
Test. java
Package com. test; import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. randomAccessFile; import java. nio. byteBuffer; import java. nio. mappedByteBuffer; import java. nio. channels. fileChannel; public class Test implements INIOTest {public void copyFileMapped (String oldPath, String newPath) throws IOException {long length = 0; RandomAccessFile raf = new RandomAccessFile (oldPath, "r "); fileChannel fcr = raf. getChannel (); length = fcr. size (); // return the ing memory block of the file to be read MappedByteBuffer mbb = fcr. map (FileChannel. mapMode. READ_ONLY, 0, length); ByteBuffer buffer = mbb. get (new byte [(int) length]); // the file to be written to RandomAccessFile raw = new RandomAccessFile (newPath, "rw"); FileChannel fcw = raw. getChannel (); MappedByteBuffer mbbw = fcw. map (FileChannel. mapMode. READ_WRITE, 0, length); for (int I = 0; I
The first is the read/write time using file ing.
The second is the time used by the file Channel to read and write files.
Time consumed by the Third bit BufferedInputStream
Channel is used for multiple tests. File ing and read/write are time-consuming and time-consuming, while BufferedInputStream is time-consuming. It is similar to ing (my personal test is a MB file, it is recommended that you use file ing for large files)
Note that file ing cannot be disabled. MappedByteBuffer is the implementation of the shared memory on the java platform. It virtualizes the hard disk into memory and is mainly used for data sharing between processes, therefore, files cannot be deleted before the process exits. And cannot access it. So set it to null and wait for garbage collection.
Reference: http://yipsilon.iteye.com/blog/298153