During IO execution, Java's InputStream is widely used, such as DataInputStream. readInt. In fact, these highly encapsulated interfaces are incredibly slow. I have a project that needs to read dictionary files of about 90 MB at startup. It takes more than 3 seconds to use DataInputStream, instead of java. the nio package directly operates on memory bytes, which can be accelerated to about MS, up to 10 times faster! Of course, the premise is that you are familiar with bitwise operations.
Java. nio provides two types of filechannels and ByteBuffer to map files to the memory. FileChannel indicates the file channel, and ByteBuffer is a buffer zone.
Procedure
① Obtain the file channel from FileInputStream, FileOutputStream, and RandomAccessFile
② Map the file memory to ByteBuffer
③ Get a byte array through the byteBuffer. array () interface
④ Direct byte operation
Sample code
The code is as follows: |
Copy code |
FileInputStream FCM = new FileInputStream (path ); // 1. Obtain the file channel from the FileInputStream object FileChannel channel = FI. getChannel (); Int fileSize = (int) channel. size (); // 2. Read the file content from the Channel ByteBuffer byteBuffer = ByteBuffer. allocate (fileSize ); // The channel. read (ByteBuffer) method is similar to inputstream. read (byte) // Each read operation reads allocate bytes to ByteBuffer. Channel. read (byteBuffer ); // Call the flip method to reverse the Buffer before reading data from the Buffer. ByteBuffer. flip (); // Read all the byte arrays contained in the current Buffer Byte [] bytes = byteBuffer. array (); ByteBuffer. clear (); // Close the channel and File Stream Channel. close (); FCM. close (); Int index = 0; Size = Utility. bytesHighFirstToInt (bytes, index ); Index + = 4;
|
If you used DataOutputStream. writeInt to save the file, you should pay attention to it when reading the file. WriteInt is written into four bytes, in which the highest is in front and the lowest is in the back, so the byte array needs to be converted to int in reverse conversion:
The code is as follows: |
Copy code |
/** * The conversion of byte arrays and integer types. It is suitable for reading writeInt data. * * @ Param bytes byte array * @ Return integer */ Public static int bytesHighFirstToInt (byte [] bytes, int start) { Int num = bytes [start + 3] & 0xFF; Num | = (bytes [start + 2] <8) & 0xFF00 ); Num | = (bytes [start + 1] <16) & 0xFF0000 ); Num | = (bytes [start] <24) & 0xFF000000 ); Return num; } |
You can also change the buffer size.
The code is as follows: |
Copy code |
Public static void copy1 (File src, File dest) throws Exception { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; Try { FileInputStream = new FileInputStream (src ); FileOutputStream = new FileOutputStream (dest ); Byte [] buffer = new byte [8096]; Int length =-1; While (length = fileInputStream. read (buffer ))! =-1 ){ FileOutputStream. write (buffer, 0, length); // write all data in the buffer at a time. FileOutputStream. flush (); } } Finally { If (fileInputStream! = Null ){ FileInputStream. close (); } If (fileOutputStream! = Null ){ FileOutputStream. close (); } } } |