Note. Increasing the heap memory limit for sorting a file with 18 lines is just a lazy way to solve a programming problem, this philosophy always increases memory instead of solving a real problem - this is the reason Java programs are not well aware of slowness and the like.
My advice is to avoid increasing memory for such a task, you need to split the file line by line and concatenate the lines in a way that is similar to MergeSort. Thus, your program can scale if the file size grows.
To split a file into several "line subscript files", use the read method of the BufferedReader class:
private void splitBigFile() throws IOException { // A 10 Mb buffer size is decent enough final int BUFFER_SIZE = 1024 * 1024 * 10; try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) { String line; int fileIndex = 0; FileWriter currentSplitFile = new FileWriter(new File("test_split.txt." + fileIndex)); char buffer[] = new char[BUFFER_SIZE]; int readed = 0; while ((readed = br.read(buffer)) != -1) { // Inspect the buffer in search of the new line character boolean endLineProcessed = false; for (int i = 0; i < readed; i++) { if (buffer[i] == '\n') { // This chunk contains the new line character, write this last chunk the current file and create a new one currentSplitFile.write(buffer, 0, i); fileIndex++; currentSplitFile = new FileWriter(new File("test_split.txt." + fileIndex)); currentSplitFile.write(buffer, i, readed - i); endLineProcessed = true; } } // If not end of line found, just write the chunk if (!endLineProcessed) { currentSplitFile.write(buffer, 0, readed); } } } }
To combine them, open all the files and save a separate buffer (small, for example, 2 mb each) for each of them, read the first fragment of each file and there, you will have enough information to start reordering the file index. Continue reading snippets if some files have links.
source share