import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* 2020年11月16日15点
* 作者 石宗昊
* io读两个文件,生成list 排重后写文件
*/
public class test10 {
public static void main(String[] args) throws IOException {
List<String> list = new ArrayList<String>();
list = file1("C:\\Users\\admin\\Desktop\\test\\df.txt");
list.removeAll(file2("C:\\Users\\admin\\Desktop\\test\\wf.txt"));
System.out.println("file1-fil2=" + list.size());
//写文件
Input(list, "C:\\Users\\admin\\Desktop\\test\\123.txt");
System.out.println("PrintWriter_OK");
}
//传入 "073126" "17737134329" "2020-10-28 00:06:38" "2020-10-28 00:06:47" "DELIVRD" "DELIVRD" "ninde" 文件地址
public static List<String> file1(String url) throws IOException {
//用来存放经过处理最后需要比对的
List<String> list = new ArrayList<String>();
//传入要读目标地址
File file = new File(url);
//生成io流
FileReader fileReader = new FileReader(file);
//接逐行数据
BufferedReader br = new BufferedReader(fileReader);
//判断文件是否存在
if (file.exists()) {
//暂存每行内容
String lineContent = null;
while ((lineContent = br.readLine()) != null) {
//截取 格式为 17799358256,2020-10-28 00:06:55
list.add(lineContent.substring(10, 43).replace("\"\t\"", ","));
}
}
//关闭读文件流
br.close();
fileReader.close();
System.out.println("file1:" + list.size());
return list;
}
//传入 18074522410,1,2020-10-28 00:06:45,3,DELIVRD,0 文件地址
public static List<String> file2(String url) throws IOException {
//传入要读目标地址
File file = new File(url);
//生成io流
FileReader fileReader = new FileReader(file);
//接逐行数据
BufferedReader br = new BufferedReader(fileReader);
//用来接 18074522410,1,2020-10-28 00:06:45,3,DELIVRD,0 号码与日期中间的数字
int a2;
//用来存放经过处理最后需要比对的
List<String> list = new ArrayList<String>();
//判断文件是否存在
if (file.exists()) {
//暂存每行内容
String lineContent = null;
//如果有下一行
while ((lineContent = br.readLine()) != null) {
//如果读到的行 有DELIVRD字段
if (lineContent.contains("DELIVRD")) {
//拿到需要重复的行数
a2 = Integer.parseInt((lineContent.substring(12, 13)));
//重复次数
for (int i = 0; i < a2; i++) {
//循环写入号段,日期 //18115170627,2020-10-28 00:07:02
list.add(lineContent.substring(0, 11) + "," + lineContent.substring(14, 33));
}
}
}
}
//关闭读文件流
br.close();
fileReader.close();
System.out.println("file2:" + list.size());
return list;
}
//将文件写入本地用的 写入内容 写入地址
public static synchronized void Input(List<String> list, String url) {
FileWriter fw = null;
try {
//如果文件存在,则追加内容;如果文件不存在,则创建文件
File f = new File(url);
fw = new FileWriter(f, true);
PrintWriter pw = new PrintWriter(fw);
for (String s : list) {
pw.println(s);
}
pw.flush();
fw.flush();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}