จากตัวอย่างโค้ดที่ให้มาถ้าเราต้องการจะเขียน main class เพื่อเรียก method readersAsText ของ class Diff
สามารถทำได้หลายแบบ ที่ผมทำคือผมตั้งชื่อ class ใหม่ชื่อ CallDiff
Code (Java)
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
public class CallDiff {
public static void main(String[] args){
try{
String fileName1 = "c:/temp/file1.txt";
String fileName2 = "c:/temp/file2.txt";
Reader reader1 = new FileReader(fileName1);
Reader reader2 = new FileReader(fileName1);
List<String> diffResult = new ArrayList<String>();
Diff.readersAsText(reader1, fileName1, reader2, fileName2, diffResult);
for (String result : diffResult){
System.out.println(result+"\n");
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
ส่วนตัวอย่าง ผลลัพธ์ ที่แนบมาให้ดู ผมงงอย่างแรง ถ้าโจทย์คือต้องการหาบรรทัดที่ diff
บรรทัดที่ 4 (b a d c e f) ก็ไม่ซ้ำกับใครเลย แต่ทำไมไม่อยู่ในผลลัพธ์ อะคับ?
private static ArrayList<String> listLine = new ArrayList<String>();
public static void main(String[] args) throws FileNotFoundException, IOException {
File file = new File("C:\\Users\\Hadoop\\Documents\\Data Apriori\\icmp_full3.txt");
FileReader fr = new FileReader(file);
LineNumberReader lnr = new LineNumberReader(fr);
String line = "";
while ((line = lnr.readLine()) != null) {
listLine.add(line);
}
for (int x = 0; x < listLine.size(); x++) {
System.out.println(listLine.get(x));
}
}
}
ผลลัพธ์เมื่อ Run ครับ
a b c d e f
e f g h i j k
a b c d e f
b a d c e f
e f a c j h
h f a c e j