-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyDiff.java
More file actions
executable file
·55 lines (52 loc) · 1.83 KB
/
MyDiff.java
File metadata and controls
executable file
·55 lines (52 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Set;
public class MyDiff {
private static void usage() {
System.err.println("usage: MyDiff <file1> <charset1> <file2> <charset2>");
System.exit(1);
}
public static void main(final String[] args) throws IOException {
if (args.length != 4) {
usage();
}
final Charset charset1 = Charset.forName(args[1]);
final Charset charset2 = Charset.forName(args[3]);
try (BufferedReader reader1 = Files.newBufferedReader(
new File(args[0]).toPath(), charset1);
BufferedReader reader2 = Files.newBufferedReader(
new File(args[2]).toPath(), charset2)) {
final Set<String> set1 = new HashSet<>();
final Set<String> set2 = new HashSet<>();
while (reader1.ready()) {
final String line = reader1.readLine().trim();
if (!line.isEmpty()) {
set1.add(line);
}
}
while (reader2.ready()) {
final String line = reader2.readLine().trim();
if (!line.isEmpty()) {
set2.add(line);
}
}
for (String e2: set2) {
if (set1.contains(e2)) {
System.out.println("<>" + e2);
set1.remove(e2);
set2.remove(e2);
}
}
for (String e1: set1) {
System.out.println("<" + e1);
}
for (String e2: set2) {
System.out.println(">" + e2);
}
}
}
}