Memory ing file for java_croe learning notes new IO-java.nio

Source: Internet
Author: User
Tags crc32

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
  1. PackageTwelve;
  2. ImportJava. Io. bufferedinputstream;
  3. ImportJava. Io. fileinputstream;
  4. ImportJava. Io. filenotfoundexception;
  5. ImportJava. Io. ioexception;
  6. ImportJava. Io. inputstream;
  7. ImportJava. Io. randomaccessfile;
  8. ImportJava. NiO. mappedbytebuffer;
  9. ImportJava. NiO. channels. filechannel;
  10. ImportJava.util.zip. CRC32;
  11. /**
  12. @ Title niottest. Java
  13. @ Description todo
  14. @ Author qinpeng
  15. @ Date Aug 25,200 9 10:23:26
  16. */
  17. Public ClassNiottest {
  18. Public Static VoidMain (string [] ARGs ){
  19. String filename = "D: // iotest.pdf ";
  20. System. Out. println ("inputstream ");
  21. LongStart = system. currenttimemillis ();
  22. LongCrcvalue = checksuminputstreanm (filename );
  23. LongEnd = system. currenttimemillis ();
  24. System. Out. println (long. tohexstring (crcvalue ));
  25. System. Out. println (end-Start) + "Time consumed ");
  26. System. Out. println ("bufferedinputstream ");
  27. Start = system. currenttimemillis ();
  28. Crcvalue = checksuminputstreanm (filename );
  29. End = system. currenttimemillis ();
  30. System. Out. println (long. tohexstring (crcvalue ));
  31. System. Out. println (end-Start) + "Time consumed ");
  32. System. Out. println ("randomaccessfileinputstream ");
  33. Start = system. currenttimemillis ();
  34. Crcvalue = checksuminputstreanm (filename );
  35. End = system. currenttimemillis ();
  36. System. Out. println (long. tohexstring (crcvalue ));
  37. System. Out. println (end-Start) + "Time consumed ");
  38. System. Out. println ("mappedfile inputstream ");
  39. Start = system. currenttimemillis ();
  40. Crcvalue = checksuminputstreanm (filename );
  41. End = system. currenttimemillis ();
  42. System. Out. println (long. tohexstring (crcvalue ));
  43. System. Out. println (end-Start) + "Time consumed ");
  44. }
  45. Public Static LongChecksuminputstreanm (string filename ){
  46. CRC32 CRC =NewCRC32 ();
  47. Try{
  48. Inputstream in =NewFileinputstream (filename );
  49. IntC;
  50. While(C = in. Read ())! =-1 ){
  51. CRC. Update (C );
  52. }
  53. }Catch(Filenotfoundexception e ){
  54. E. printstacktrace ();
  55. System. Err. Print ("niottest -- checksuminputstreanm -- New fileinputstream is not found ");
  56. }Catch(Ioexception IOE ){
  57. IOE. printstacktrace ();
  58. System. Err. Print ("niottest -- checksuminputstreanm -- New fileinputstream 'read append ioexception ");
  59. }
  60. ReturnCRC. getvalue ();
  61. }
  62. Public Static LongChecksumbufferedinputstream (string filename ){
  63. CRC32 CRC =NewCRC32 ();
  64. Try{
  65. Inputstream in =NewBufferedinputstream (NewFileinputstream (filename ));
  66. IntC;
  67. While(C = in. Read ())! =-1 ){
  68. CRC. Update (C );
  69. }
  70. }Catch(Filenotfoundexception e ){
  71. E. printstacktrace ();
  72. System. Err. Print ("niottest -- checksumbufferedinputstream -- New fileinputstream is not found ");
  73. }Catch(Ioexception IOE ){
  74. IOE. printstacktrace ();
  75. System. Err. Print ("niottest -- checksumbufferedinputstream -- New fileinputstream 'read append ioexception ");
  76. }
  77. ReturnCRC. getvalue ();
  78. }
  79. Public Static LongChecksumrondomaccessfileinputstream (string filename ){
  80. CRC32 CRC =NewCRC32 ();
  81. Try{
  82. Randomaccessfile file =NewRandomaccessfile (filename, "R ");
  83. IntC;
  84. While(C = file. Read ())! =-1 ){
  85. CRC. Update (C );
  86. }
  87. }Catch(Filenotfoundexception e ){
  88. E. printstacktrace ();
  89. System. Err. Print ("niottest -- checksumrondomaccessfileinputstream -- New fileinputstream is not found ");
  90. }Catch(Ioexception IOE ){
  91. IOE. printstacktrace ();
  92. System. Err. Print ("niottest -- checksumrondomaccessfileinputstream -- New fileinputstream 'read append ioexception ");
  93. }
  94. ReturnCRC. getvalue ();
  95. }
  96. Public Static LongChecksummappedfile (string filename ){
  97. CRC32 CRC =NewCRC32 ();
  98. Try{
  99. Fileinputstream in =NewFileinputstream (filename );
  100. Filechannel channel = in. getchannel ();
  101. IntLength = (Int) Channel. Size ();
  102. Mappedbytebuffer buffer = channel. Map (filechannel. mapmode. read_only, 0, length );
  103. For(IntP = 0; P <length; P ++ ){
  104. IntC = buffer. getint (P );
  105. CRC. Update (C );
  106. }
  107. }Catch(Filenotfoundexception e ){
  108. E. printstacktrace ();
  109. System. Err. Print ("niottest -- checksumrondomaccessfileinputstream -- New fileinputstream is not found ");
  110. }Catch(Ioexception IOE ){
  111. IOE. printstacktrace ();
  112. System. Err. Print ("niottest -- checksumrondomaccessfileinputstream -- New fileinputstream 'read append ioexception ");
  113. }
  114. ReturnCRC. getvalue ();
  115. }
  116. }

------------------------------------------------]

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 ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.