I want to compare contens of my two txt files and write the different words in other file3.txt file
I want to do compare method in this way to write another txt file. A
I just ran your program with the following files and could not reproduce your problem.
deneme1
abc
def
ghi
deneme2
abc
ghi
klm
And deneme3 was created with the following content:
abc
ghi
EDIT
It seems you want the opposite behaviour. Some of your methods are unnecessarily complicated and could be made much shorter by using the right tools of the standard JDK. See below an example of a simplified implementation (that only keeps the words that are not in common between the 2 files) - this example is case sensitive:
public class TextAreaSample {
public static void main(String[] args) throws IOException {
//readAllLines does what you do in readFileAsList
List strings1 = Files.readAllLines(Paths.get("C:/temp/deneme1.txt"), Charset.defaultCharset());
List strings2 = Files.readAllLines(Paths.get("C:\\temp\\deneme2.txt"), Charset.defaultCharset());
Set notInCommon = getNotInCommon(strings1, strings2);
write(notInCommon, "C:\\temp\\deneme3.txt");
}
private static void write(Collection out, String fname) throws IOException {
FileWriter writer = new FileWriter(new File("C:\\temp\\deneme3.txt"));
for (String s : out) {
writer.write(s + "\n");
}
writer.close();
}
private static Set getNotInCommon(List strings1, List strings2) {
//Sets are great to get unique lists and check commonality
Set onlyInFile1 = new HashSet(strings1);
onlyInFile1.removeAll(strings2); //remove strings in s1 AND s2
Set onlyInFile2 = new HashSet(strings2);
onlyInFile2.removeAll(strings1); //remove strings in s1 AND s2
Set notInCommon = new HashSet<>();
notInCommon.addAll(onlyInFile1);
notInCommon.addAll(onlyInFile2);
return notInCommon;
}
}