1

i want to do the content of 1.txt minus 2.txt and output that in 3.txt. Also if 2.txt has extra remaining lines, output it in 4.txt.

The content of the text files are not ordered line by line, so basically the code needs to remove the matching line only, like this:

1.txt contains:

example1
example2
example3

2.txt contains:

example2
example3
example4

3.txt will contain:

example1

4.txt will contain:

example4
Dark Soul
  • 83
  • 5
  • 14
  • You need to break this problem down into steps. Think about how to read and write files line by line. Once you can read a file line by line and write it back out, think about how to compare it with another file. From these steps the answer to your homework assignment will work out. – Michael Shopsin Sep 18 '14 at 14:53
  • http://stackoverflow.com/q/7107517/4052745 Did you take a look at this? – Matt C. Sep 18 '14 at 14:54
  • Use some [Sets](http://en.wikipedia.org/wiki/Set_(mathematics)#Basic_operations). – NESPowerGlove Sep 18 '14 at 14:55

1 Answers1

1

Using Guava:

Set<String> lines1 = 
    new HashSet<>(Files.readLines(new File("1.txt"), Charsets.UTF_8));
Set<String> lines2 = 
    new HashSet<>(Files.readLines(new File("2.txt"), Charsets.UTF_8));
Set<String> minus1 = Sets.difference(lines1, lines2);
Set<String> minus2 = Sets.difference(lines2, lines1);
Files.asCharSink(new File("3.txt"), Charsets.UTF_8).writeLines(minus1);
Files.asCharSink(new File("4.txt"), Charsets.UTF_8).writeLines(minus2);
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118