1. In Java's traditional IO system, the process of reading disk file data is as follows:
Take the FileInputStream class as an example, the class has a read (byte b[]) method, and byte b[] is the buffer we want to store the read to user space. Refer to the source of the read (byte b[]) method, it will call the Readbytes (b, 0, B.length) method internally, and the Readbytes (b, 0, B.length) method is a native method (that is, the local method), Finally, this local method is used to initiate a system call, called the Read () method of the system kernel, which reads data from the disk into the kernel buffer, which is read from disk by the disk controller through DMA operations to the kernel buffer, which is not dependent on the CPU. The user process then copies the data from the kernel buffer to the user space buffer. The user process then reads the data from the user space buffer. Because the user process is not directly accessible to the hardware. Therefore, it is necessary to use the kernel to act as a middleman to realize the file reading. The entire process is as follows:
2. Since Java 1.4, Java has introduced the concept of file channels in NiO, and a FileChannel class has been provided in the API. This class is associated with a traditional IO stream. The file channel can be obtained by fileinputstream or fileoutputstream, and we can read and write the file through the channel.
3. The concept of file memory mapping is also introduced in JAVA NIO: Most modern operating systems support virtual memory mapping so that we can map the kernel space address to the same physical address as the virtual address of the user space, so that the DMA Hardware (access to physical memory addresses only) can populate buffers that are visible to both the kernel and the user-space process. As shown in the following:
The advantage of this is that when we read the disk file, we no longer have to go back and forth through the kernel buffer to the user process buffer. The operating system loads the disk files into the physical memory that is cached for the paging area through some page scheduling algorithms. We can read the disk file by mapping the physical memory.
4. Below we verify the role of the file channel and the file memory mapping in the IO system through three different ways of file copy. The test environment is for Windows 32-bit systems and JDK1.6. The test file used in the code is Movie.avi to a 123MB video file. The code is as follows:
1 PackageCn.com.hbust.nio.file;2 3 ImportJava.io.File;4 5 ImportJava.io.FileInputStream;6 7 ImportJava.io.FileOutputStream;8 9 ImportJava.nio.MappedByteBuffer;Ten One ImportJava.nio.channels.FileChannel; A - - the Public classFilecopytest { - - - + Public Static voidMain (string[] args)throwsException { - +String SourcePath = "F:\\mywork\\javademo\\dir1\\movie.avi"; A atString destPath1 = "F:\\mywork\\javademo\\dir2\\movie1.avi"; - -String destPath2 = "F:\\mywork\\javademo\\dir2\\movie2.avi"; - -String destPath3 = "F:\\mywork\\javademo\\dir2\\movie3.avi"; - in LongT1 =System.currenttimemillis (); - to traditionalcopy (sourcepath,destpath1); + - Longt2 =System.currenttimemillis (); the *System.out.println ("Traditional Io method for file copy time:" + (T2-T1) + "MS"); $ Panax Notoginseng - the niocopy (sourcepath,destpath2); + A LongT3 =System.currenttimemillis (); the +System.out.println ("Using the NiO file channel method for file copy time:" + (T3-T2) + "MS"); - $ $ - nioCopy2 (SOURCEPATH,DESTPATH3); - the LongT4 =System.currenttimemillis (); - WuyiSystem.out.println ("Time to file copy using NiO file memory mapping and File channel:" + (T4-T3) + "MS"); the - Wu - } About $ - - Private Static voidNioCopy2 (String SourcePath, String destpath)throwsException { - AFile Source =NewFile (sourcepath); + theFile dest =NewFile (destpath); - $ if(!dest.exists ()) { the the dest.createnewfile (); the the } - inFileInputStream FIS =NewFileInputStream (source); the theFileOutputStream fos =NewFileOutputStream (dest); About theFileChannel Sourcech =Fis.getchannel (); the theFileChannel Destch =Fos.getchannel (); + -Mappedbytebuffer MBB = Sourcech.map (FileChannel.MapMode.READ_ONLY, 0, Sourcech.size ()); the Bayi Destch.write (MBB); the the sourcech.close (); - - destch.close (); the the } the the - the the the Private Static voidTraditionalcopy (String SourcePath, String destpath)throwsexception{94 theFile Source =NewFile (sourcepath); the theFile dest =NewFile (destpath);98 About if(!dest.exists ()) { - 101 dest.createnewfile ();102 103 } 104 theFileInputStream FIS =NewFileInputStream (source);106 107FileOutputStream fos =NewFileOutputStream (dest);108 109 byte[] buf = newbyte [512]; the 111 intLen = 0; the 113 while(len = Fis.read (BUF))! =-1) { the theFos.write (buf, 0, Len); the 117 } 118 119 fis.close (); - 121 fos.close ();122 123 } 124 the 126 127 Private Static voidNiocopy (String SourcePath, String destpath)throwsexception{ - 129File Source =NewFile (sourcepath); the 131File dest =NewFile (destpath); the 133 if(!dest.exists ()) { 134 135 dest.createnewfile ();136 137 } 138 139FileInputStream FIS =NewFileInputStream (source); $ 141FileOutputStream fos =NewFileOutputStream (dest);142 143FileChannel Sourcech =Fis.getchannel ();144 145FileChannel Destch =Fos.getchannel ();146 147Destch.transferfrom (Sourcech, 0, Sourcech.size ()); 148 149 sourcech.close (); Max 151 destch.close (); the 153 } 154 155}
After each copy is executed, the contents of the F:\mywork\javademo\dir2\ directory are deleted and executed 8 times. Observe the test results as follows: The time unit is MS (MS)
From the above table, the traditional IO mode average copy completion time is about 1968ms,nio file Channel mode average copy completion time is about 1672ms, file memory mapping and file channel mode average copy completion time is about 1418ms.
Reprinted from Http://www.open-open.com/lib/view/open1413518521372.html
Analysis on performance difference of Java IO and Java nio on file copy