合并两个文件,并且排除掉重复的内容

/**

*说明:文本内容以换行为一组

*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class MergeFile {

public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("输入合成文件的路径及文件名:");
String newFile = scanner.nextLine();
File mergeFile = new File(newFile);
if(!mergeFile.exists()){
mergeFile.createNewFile();
}
System.out.println("输入第一个文件的路径及文件名:");
String file1 = scanner.nextLine();

System.out.println("输入第二个文件的路径及文件名");
String file2 = scanner.nextLine();
scanner.close();
List list1 = readFileByLine(file1);
List list2 = readFileByLine(file2);
FileWriter fw = new FileWriter(mergeFile,true);
for (String s1 : list1) {
for (String s2 : list2) {
if(s1.equals(s2)){
System.out.println("same:"+s2);
continue;
}
}
fw.write(s1+System.getProperty("line.separator"));
}
for (String s2 : list2) {
fw.write(s2+System.getProperty("line.separator"));
}
fw.close();
}

public static List readFileByLine(String fileName) throws Exception{
InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName),"UTF-8");
BufferedReader br = new BufferedReader(isr);
String s = "";
List existEnterprise = new ArrayList();
while((s=br.readLine())!=null){
existEnterprise.add(s);
}
isr.close();
br.close();
return existEnterprise;
}


}

你可能感兴趣的:(工具类)