Released on: 23:37:54 Author: Source: javaeye blog
Http://12616383.javaeye.com/blog/457582
Refer:
No format input stream 110 seconds
Buffered input stream: 9.9 seconds
Random File Access: 162 seconds
Memory ing file: 7.2 seconds
Example
Java code
- PackageTwelve;
- ImportJava. Io. bufferedinputstream;
- ImportJava. Io. fileinputstream;
- ImportJava. Io. filenotfoundexception;
- ImportJava. Io. ioexception;
- ImportJava. Io. inputstream;
- ImportJava. Io. randomaccessfile;
- ImportJava. NiO. mappedbytebuffer;
- ImportJava. NiO. channels. filechannel;
- ImportJava.util.zip. CRC32;
- /**
- @ Title niottest. Java
- @ Description todo
- @ Author qinpeng
- @ Date Aug 25,200 9 10:23:26
- */
- Public ClassNiottest {
- Public Static VoidMain (string [] ARGs ){
- String filename = "D: // iotest.pdf ";
- System. Out. println ("inputstream ");
- LongStart = system. currenttimemillis ();
- LongCrcvalue = checksuminputstreanm (filename );
- LongEnd = system. currenttimemillis ();
- System. Out. println (long. tohexstring (crcvalue ));
- System. Out. println (end-Start) + "Time consumed ");
- System. Out. println ("bufferedinputstream ");
- Start = system. currenttimemillis ();
- Crcvalue = checksuminputstreanm (filename );
- End = system. currenttimemillis ();
- System. Out. println (long. tohexstring (crcvalue ));
- System. Out. println (end-Start) + "Time consumed ");
- System. Out. println ("randomaccessfileinputstream ");
- Start = system. currenttimemillis ();
- Crcvalue = checksuminputstreanm (filename );
- End = system. currenttimemillis ();
- System. Out. println (long. tohexstring (crcvalue ));
- System. Out. println (end-Start) + "Time consumed ");
- System. Out. println ("mappedfile inputstream ");
- Start = system. currenttimemillis ();
- Crcvalue = checksuminputstreanm (filename );
- End = system. currenttimemillis ();
- System. Out. println (long. tohexstring (crcvalue ));
- System. Out. println (end-Start) + "Time consumed ");
- }
- Public Static LongChecksuminputstreanm (string filename ){
- CRC32 CRC =NewCRC32 ();
- Try{
- Inputstream in =NewFileinputstream (filename );
- IntC;
- While(C = in. Read ())! =-1 ){
- CRC. Update (C );
- }
- }Catch(Filenotfoundexception e ){
- E. printstacktrace ();
- System. Err. Print ("niottest -- checksuminputstreanm -- New fileinputstream is not found ");
- }Catch(Ioexception IOE ){
- IOE. printstacktrace ();
- System. Err. Print ("niottest -- checksuminputstreanm -- New fileinputstream 'read append ioexception ");
- }
- ReturnCRC. getvalue ();
- }
- Public Static LongChecksumbufferedinputstream (string filename ){
- CRC32 CRC =NewCRC32 ();
- Try{
- Inputstream in =NewBufferedinputstream (NewFileinputstream (filename ));
- IntC;
- While(C = in. Read ())! =-1 ){
- CRC. Update (C );
- }
- }Catch(Filenotfoundexception e ){
- E. printstacktrace ();
- System. Err. Print ("niottest -- checksumbufferedinputstream -- New fileinputstream is not found ");
- }Catch(Ioexception IOE ){
- IOE. printstacktrace ();
- System. Err. Print ("niottest -- checksumbufferedinputstream -- New fileinputstream 'read append ioexception ");
- }
- ReturnCRC. getvalue ();
- }
- Public Static LongChecksumrondomaccessfileinputstream (string filename ){
- CRC32 CRC =NewCRC32 ();
- Try{
- Randomaccessfile file =NewRandomaccessfile (filename, "R ");
- IntC;
- While(C = file. Read ())! =-1 ){
- CRC. Update (C );
- }
- }Catch(Filenotfoundexception e ){
- E. printstacktrace ();
- System. Err. Print ("niottest -- checksumrondomaccessfileinputstream -- New fileinputstream is not found ");
- }Catch(Ioexception IOE ){
- IOE. printstacktrace ();
- System. Err. Print ("niottest -- checksumrondomaccessfileinputstream -- New fileinputstream 'read append ioexception ");
- }
- ReturnCRC. getvalue ();
- }
- Public Static LongChecksummappedfile (string filename ){
- CRC32 CRC =NewCRC32 ();
- Try{
- Fileinputstream in =NewFileinputstream (filename );
- Filechannel channel = in. getchannel ();
- IntLength = (Int) Channel. Size ();
- Mappedbytebuffer buffer = channel. Map (filechannel. mapmode. read_only, 0, length );
- For(IntP = 0; P <length; P ++ ){
- IntC = buffer. getint (P );
- CRC. Update (C );
- }
- }Catch(Filenotfoundexception e ){
- E. printstacktrace ();
- System. Err. Print ("niottest -- checksumrondomaccessfileinputstream -- New fileinputstream is not found ");
- }Catch(Ioexception IOE ){
- IOE. printstacktrace ();
- System. Err. Print ("niottest -- checksumrondomaccessfileinputstream -- New fileinputstream 'read append ioexception ");
- }
- ReturnCRC. getvalue ();
- }
- }
------------------------------------------------]
With the memory ing file, you can think that all the files have been read into the memory, and then use it as a very large array for access. This solution greatly simplifies the code for modifying files. The following is a simple example:
Code
Import java. Io .*;
Import java. NiO .*;
Import java. NiO. channels .*;
Public class largemappedfiles {
Static int length = 0x8ffffff; // 128 MB
Public static void main (string [] ARGs) throws exception {
Mappedbytebuffer out =
New randomaccessfile ("test. dat", "RW"). getchannel (). Map (filechannel. mapmode. read_write, 0, length );
For (INT I = 0; I <length; I ++)
Out. Put (byte) 'X ');
System. Out. println ("finished writing ");
For (INT I = length/2; I <length/2 + 6; I ++)
System. Out. Print (char) Out. Get (I ));
}
}
To open a file in read/write mode, we start with randomaccessfile.
After obtaining the channel, we use the map () method to generate a mappedbytebuffer. This is a special "direct buffer ". Note: You must specify the position where the file is mapped and the ing range is large. That is, it can also map a small part of a large file.
Mappedbytebuffer is a derived class of bytebuffer, so it has all the methods of bytebuffer. Here we only briefly demonstrate the put () and get () methods. In addition, you can also use methods such as ascharbuffer.
The preceding example creates a MB file, which may be out of the operating system's permitted range. File Access seems to be a matter of instant, because only a small part of the memory is actually transferred, and the rest is placed on the swap file. In this way, you can easily modify super large files (up to 2 GB ).