今天需要读取两个文件中的数据,找出文件2中存在而文件1中不存在的数据,读取内容加入set集合,使用set的removeall找出差集。写入到新的txt中。
测试代码:
package connect.util;
import java.io.*;
import java.util.HashSet;
import java.util.Set;
/**
* 读取txt test
*/
public class ReadTxtFile {
public static void readTxt(String filePath,String filePath2) {
try {
File file = new File(filePath);
File file2 = new File(filePath2);
if(file.isFile() && file.exists()) {
Set<String> set1 =new HashSet<String>();
Set<String> set2 =new HashSet<String>();
Set<String> set3 =new HashSet<String>();
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
InputStreamReader isr2 = new InputStreamReader(new FileInputStream(file2), "utf-8");
BufferedReader br = new BufferedReader(isr);
BufferedReader br2 = new BufferedReader(isr2);
String lineTxt = null;
String lineTxt2 = null;
while ((lin