-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckfiles.java
50 lines (37 loc) · 980 Bytes
/
checkfiles.java
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
/*
* checkfiles.java (check if two text files are the same)
*/
import java.util.*;
import java.io.*;
public class checkfiles {
public static void main(String[] args) throws IOException {
Scanner f1 = new Scanner(new File(args[0]));
Scanner f2 = new Scanner(new File(args[1]));
boolean isTheSame = true;
int cnt = 1;
while(true) {
if( f1.hasNext() && f2.hasNext() ) {
String line1 = f1.nextLine().trim();
String line2 = f2.nextLine().trim();
if( !line1.equals(line2) ) {
isTheSame = false;
break;
}
}
else if ( f1.hasNext() && !f2.hasNext() || !f1.hasNext() && f2.hasNext() ) {
isTheSame = false;
break;
}
else //both files have finished
break;
cnt++;
}
f1.close();
f2.close();
System.out.printf("%-20s", args[1]);
if(isTheSame)
System.out.println("\tsame\n");
else
System.out.println("\tdiff at line " + cnt + "\n");
}
}