u25
This commit is contained in:
parent
4af4b837b7
commit
f929623e0b
Binary file not shown.
|
@ -0,0 +1,68 @@
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Klasse zum Vergleichen von Dateien
|
||||||
|
*/
|
||||||
|
public class Diff {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
diff("f1.txt","f2.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vergleicht zwei Dateien und gibt deren Unterschiede
|
||||||
|
* Zeilenweise aus
|
||||||
|
*/
|
||||||
|
public static void diff(String filename1, String filename2) {
|
||||||
|
Scanner scan1 = openFile(filename1);
|
||||||
|
if (scan1 == null) return;
|
||||||
|
|
||||||
|
Scanner scan2 = openFile(filename2);
|
||||||
|
if (scan2 == null) {
|
||||||
|
scan1.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean foundDiff = false;
|
||||||
|
for ( int zeile = 1; scan1.hasNextLine() || scan2.hasNextLine(); zeile++) {
|
||||||
|
String line1 = nextLine(scan1);
|
||||||
|
String line2 = nextLine(scan2);
|
||||||
|
|
||||||
|
if (!line1.equals(line2)) {
|
||||||
|
foundDiff = true;
|
||||||
|
System.out.println("Zeile " + zeile + ":");
|
||||||
|
System.out.println("< " + line1);
|
||||||
|
System.out.println("> " + line2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!foundDiff) {
|
||||||
|
System.out.println("Keine Unterschiede gefunden");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Oeffnet eine Datei mit einem Scanner und gibt diesen
|
||||||
|
* zurueck
|
||||||
|
* Falls die Datei nicht gefunden werden kann, wird eine
|
||||||
|
* Fehlermeldung ausgegeben und der Rueckgabewert ist null
|
||||||
|
*/
|
||||||
|
public static Scanner openFile(String filename) {
|
||||||
|
try {
|
||||||
|
return new Scanner(new File(filename));
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.out.println("Kann Datei nicht finden: " + filename);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gibt die naechste Zeile eines Scanners aus
|
||||||
|
* Falls es keine naechste Zeile gibt, wird stattdessen
|
||||||
|
* ein leerer String zurueckgegeben
|
||||||
|
*/
|
||||||
|
public static String nextLine(Scanner scan) {
|
||||||
|
if (scan.hasNextLine()) return scan.nextLine();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
Yallo
|
||||||
|
Wie
|
||||||
|
Gehts
|
||||||
|
So
|
||||||
|
;)
|
||||||
|
|
||||||
|
nice
|
|
@ -0,0 +1,5 @@
|
||||||
|
Yallo
|
||||||
|
Wie
|
||||||
|
Geht's
|
||||||
|
So
|
||||||
|
;)
|
Loading…
Reference in New Issue