Java read and write large text files (2GB or more)

Source: Internet
Author: User

In the following program, a text file with a line number of filelines is divided evenly into Splitnum small text file, where the newline character ' R ' is on Linux, and Windows's Java line break is ' \ r \ n ':

  1. Package Kddcup2012.task2.FileSystem;
  2. import Java.io.BufferedInputStream;
  3. import Java.io.BufferedReader;
  4. import java.io.File;
  5. import Java.io.FileInputStream;
  6. import Java.io.FileWriter;
  7. import java.io.IOException;
  8. import Java.io.InputStreamReader;
  9. Public class Filesplit
  10. {
  11. Public static void main (string[] args) throws IOException
  12. {
  13. long timer = System.currenttimemillis ();
  14. int buffersize = * 1024x768 * 1024x768; //Set the cache for read files to 20MB
  15. //Create buffered text input stream
  16. File File = new file ("/media/data/Graduation design/kdd cup/data/userid_profile.txt");
  17. FileInputStream FileInputStream = new fileinputstream (file);
  18. Bufferedinputstream Bufferedinputstream = new bufferedinputstream (FileInputStream);
  19. InputStreamReader InputStreamReader = new InputStreamReader (Bufferedinputstream);
  20. BufferedReader input = new BufferedReader (InputStreamReader, buffersize);
  21. int splitnum = 1; //Reduce the number of blocks to split by one
  22. int filelines = 23669283; //Enter the number of lines in the file
  23. long persplitlines = Filelines/splitnum; //number of rows per block
  24. For (int i = 0; I <= splitnum; ++i)
  25. {
  26. //Split
  27. //Each block creates an output
  28. FileWriter output = new FileWriter ("/home/haoqiong/part" + i + ". txt");
  29. String line = null;
  30. //Line-by-row reading, progressive output
  31. For (long linecounter = 0; linecounter < persplitlines && (line = input . ReadLine ())! = null; ++linecounter)
  32. {
  33. Output.append (line + "\ r");
  34. }
  35. Output.flush ();
  36. Output.close ();
  37. output = null;
  38. }
  39. Input.close ();
  40. Timer = System.currenttimemillis ()-timer;
  41. System.out.println ("Processing time:" + timer);
  42. }
  43. }

The above program to handle large text files only need about 30MB of memory space (which is related to the size of the read buffer set), but the speed is not very fast, in the case of disk no other program occupied, the 200MB file is divided into 112 parts need 20 seconds (machine configuration: Centrino2 P7450 CPU, 2GB DDR3 Memory, Ubuntu 11.10 system, HDD maximum read and write speed of about 60mb/s).

In addition, for files that are hundreds of to 2GB in size, the memory-mapped file will block some, but the memory map can only process files below 2GB because the mapped file length cannot exceed the maximum value of the int type in Java.

Java read and write large text files (2GB or more)

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.