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