Java Splits a txt file evenly into N txt files by line

Source: Internet
Author: User

My requirement here is to put a TXT file of over 60 m source.txt

Split the file into four TXT files: text1.txt text2.txt text3.txt text4.txt

For example

Save the first line of source.txt to text1.txt.

Row 2 is saved to text2.txt

Row 3 is saved to text3.txt

Row 4 is saved to text4.txt

Row 5 is saved to text1.txt

...................................

...................................

...................................

 

And so on

 

Code on

 

public static void ReadData() {try {FileReader read = new FileReader("D:/data.txt");BufferedReader br = new BufferedReader(read);String row;int rownum = 1;FileWriter fw1 = new FileWriter("/text1.txt");FileWriter fw2 = new FileWriter("/text2.txt");FileWriter fw3 = new FileWriter("/text3.txt");FileWriter fw4 = new FileWriter("/text4.txt");while ((row = br.readLine()) != null) {if (rownum % 4 == 1) {fw1.append(row + "\r\n");} else if (rownum % 4 == 2) {fw2.append(row + "\r\n");} else if (rownum % 4 == 3) {fw3.append(row + "\r\n");} else if (rownum % 4 == 0) {fw4.append(row + "\r\n");}rownum++;}fw1.close();fw2.close();fw3.close();fw4.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

 

 

Note: At the beginning, I split 60 MB of all data into four strings in the memory, and saved them as TXT.

The problem arises. If this method is used, the efficiency is very low. It took 10 minutes to split the 60 m ..

Then I decisively stopped the program.

Then the above method: Read a row and save it immediately. 5 seconds...

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.