I have a PC with 4 Gigabyte RAM and a file with 10 Gigabyte memory usage. Now I want to check, if each line in the file is unique so I have written the following code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class Cleaner {
public static void main(String[] args) throws IOException {
if (args.length < 2) {
System.out.println("Too less parameters!");
return;
}
File file = new File(args[0]);
BufferedReader buff = new BufferedReader(new FileReader(file));
String line;
Set<String> set = new HashSet<String>();
while ((line = buff.readLine()) != null) {
set.add(line);
}
FileWriter fw = new FileWriter(args[1]);
for (String s : set) {
fw.write(s + "\n");
fw.flush();
}
fw.close();
buff.close();
}
}
But I get a OutOfMemoryException so my question is:
How should I change my code to get a file where each line is unique?
Thank you for your help in advance.