Java read large file operation "Go"

Source: Internet
Author: User

Transfer from http://aronlulu.iteye.com/blog/1018370

Read File size: 1.45G
The first kind, Oldio:

Java code
  1. Public static void Oldioreadfile () throws ioexception{
  2. BufferedReader br = new BufferedReader (new FileReader ("G://lily_947.txt"));
  3. PrintWriter pw = new PrintWriter ("g://oldio.tmp");
  4. char[] C = new char[100*1024*1024];
  5. for (;;) {  
  6. if (Br.read (c)!=-1) {
  7. Pw.print (c);
  8. }else{
  9. Break ;
  10. }
  11. }
  12. Pw.close ();
  13. Br.close ();
  14. }


Time Consuming 70.79s


The second kind, Newio:

Java code
  1. Public static void Newioreadfile () throws ioexception{
  2. FileChannel read = new Randomaccessfile ("G://lily_947.txt","R"). Getchannel ();
  3. FileChannel writer = new Randomaccessfile ("g://newio.tmp","RW"). Getchannel ();
  4. Bytebuffer BB = bytebuffer.allocate (200*1024*1024);
  5. While (Read.read (BB)!=-1) {
  6. Bb.flip ();
  7. Writer.write (BB);
  8. Bb.clear ();
  9. }
  10. Read.close ();
  11. Writer.close ();
  12. }


Time Consuming 47.24s


The Third Kind, randomaccessfile:

Java code
  1. Public static void Randomreadfile () throws ioexception{
  2. Randomaccessfile read = new Randomaccessfile ("G://lily_947.txt","R");
  3. Randomaccessfile writer = new Randomaccessfile ("g://random.tmp","RW");
  4. byte[] B = new byte[200*1024*1024];
  5. While (Read.read (b)!=-1) {
  6. Writer.write (b);
  7. }
  8. Writer.close ();
  9. Read.close ();
  10. }


Time 46.65

The fourth kind, Mappedbytebuffer:

Java code
  1. Public static void Mappedbuffer () throws ioexception{
  2. FileChannel read = new FileInputStream ("G://lily_947.txt"). Getchannel ();
  3. FileChannel writer = new Randomaccessfile ("g://buffer.tmp","RW"). Getchannel ();
  4. long i = 0;
  5. Long size = Read.size ()/30;
  6. Bytebuffer BB,CC = null;
  7. While (I<read.size () && (Read.size ()-i) >size) {
  8. bb = Read.map (FileChannel.MapMode.READ_ONLY, I, size);
  9. CC = Writer.map (FileChannel.MapMode.READ_WRITE, I, size);
  10. Cc.put (BB);
  11. I+=size;
  12. Bb.clear ();
  13. Cc.clear ();
  14. }
  15. bb = Read.map (FileChannel.MapMode.READ_ONLY, I, read.size ()-i);
  16. Cc.put (BB);
  17. Bb.clear ();
  18. Cc.clear ();
  19. Read.close ();
  20. Writer.close ();
  21. }


Time: 36

The resource occupancy graphs for the first three readings are as follows:
Compared to the last memory direct mapping method before the test actually meaningless, basic second kill ....
There is not enough memory for a large file block map directly, because Mappedbytebuffer is not released, Sun does not provide a way to reclaim the Mappedbytebuffer area directly, and this time there are two ways to solve it, the first of which is rather stupid:

Java code
    1. System.GC ();
    2. System.runfinalization ();
    3. try {
    4. Thread.Sleep (3000);
    5. } catch (Interruptedexception e) {
    6. E.printstacktrace ();
    7. }


The second kind of online search, using reflection to call the Clean method:

Java code
  1. Public static void Unmap (final mappedbytebuffer buffer) {
  2. if (buffer = = null) {
  3. return;
  4. }
  5. Accesscontroller.doprivileged (new privilegedaction<object> () {
  6. Public Object Run () {
  7. try {
  8. Method Getcleanermethod = Buffer.getclass (). GetMethod ("cleaner", new class[0]);
  9. if (getcleanermethod! = null) {
  10. Getcleanermethod.setaccessible (true);
  11. Object cleaner = getcleanermethod.invoke (buffer, new object[0]);
  12. Method Cleanmethod = Cleaner.getclass (). GetMethod ("clean", new class[0]);
  13. if (cleanmethod! = null) {
  14. Cleanmethod.invoke (cleaner, new object[0]);
  15. }
  16. }
  17. } catch (Exception e) {
  18. E.printstacktrace ();
  19. }
  20. return null;
  21. }
  22. });
  23. }


The above two methods feel awkward, there is the ability to separate into physical files to recycle calls, this is not too beautiful.
The speed will also slow down a lot.

Java read large file operation "Go"

Related Article

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.