/**
* 根据子文件内容来筛选父文件内容
* 将筛出来的数据写入指定的txt文件中
*
* @param fileA 父文件
* @param fileB 子文件
* @param pathC 上传的数据筛选路径
* @param pathD 增量的数据筛选路径
*/
public static void screenData(String fileA, String fileB, String pathC, String pathD) {
try {
//开始时间
long startTime = System.currentTimeMillis();
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(pathC, true)));
BufferedWriter outD = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(pathD, true)));
File fA = new File(fileA); //父文件
File fB = new File(fileB); //子文件
File fC = new File(pathC); //目标文件
//文件流
FileInputStream fisA = new FileInputStream(fA);
FileInputStream fisB = new FileInputStream(fB);
//输入流
InputStreamReader isrA = new InputStreamReader(fisA, "utf-8");
InputStreamReader isrB = new InputStreamReader(fisB, "utf-8");
//缓存
BufferedReader brA = new BufferedReader(isrA);
BufferedReader brB = new BufferedReader(isrB);
List<String> tA = new ArrayList<>(); //父文件
List<String> tB = new ArrayList<>(); //子文件
List<String> tC = new ArrayList<>(); //已上传的文件
// List<String> tD = new ArrayList<>(); //未上传的文件
String line = "";
//如果没有目标文件,则创建目标文件
if (!fC.exists()) {
fC.createNewFile();
}
while ((line = brB.readLine()) != null) {
tB.add(line);
}
while ((line = brA.readLine()) != null) {
tA.add(line);
}
//将已上传的数据加入文档中
for (String a : tA) {
for (String b : tB) {
if (a.contains(b)) {
tC.add(a);
// out.write(a + '\n');
}
}
}
List<String> lists = removalDuplicate(tC);
for (String list : lists) {
out.write(list + '\n');
}
tA.removeAll(tC);
for (String a : tA) {
outD.write(a+'\n');
}
out.close();
outD.close();
long endTime = System.currentTimeMillis();
System.out.println("程序执行时间:" + (endTime - startTime) + "ms");
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
/**
* list集合元素去重
* @param data 要去重的list集合
*/
public static List<String> removalDuplicate(List<String> data) {
String str = "";
List<String> list = new ArrayList<>();
for (String datum : data) {
Matcher matcher = Pattern.compile("(\\\\)(\\d)+.jpg").matcher(datum);
if (matcher.find()) {
str = datum.replace(matcher.group(), "");
list.add(str);
}
// System.out.println("截取后的字符串:" + str);
}
LinkedHashSet<String> hashSet = new LinkedHashSet<>(list);
ArrayList<String> listWithoutDuplicates = new ArrayList<>(hashSet);
// System.out.println("去重后的数据:"+listWithoutDuplicates);
System.out.println("去重前的数量:" + data.size());
System.out.println("去重后的数量:"+listWithoutDuplicates.size());
return listWithoutDuplicates;
}