一:示例背景介绍:
本示例为了处理里相同格式的A,B两个txt 文件,筛选出文件中的不同即有三种情况:
1.A 中有的,B中没有的 ;
2.B中有的,A中没有的;
3.A,B两者都有的,不过值不完全相同;
二:具体实现:
文本格式形如:
java 代码如下:
java bean :
java 方法:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TextRead {
public static void readTxtFile(List<FundBean> list,Map<String,FundBean> map,String filePath){
// List<FundBean> list = new ArrayList<FundBean>();
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String text = "";
while((text = bufferedReader.readLine())!= null){
System.out.println(text);
if(!("".equals(text))){
FundBean obj = new FundBean();
String[] ss = text.split("\\|");
obj.setCustId(ss[0]);
obj.setTradeAccount(ss[1]);
obj.setFundCode(ss[2]);
//obj.setCountFen(Double.parseDouble(ss[3]));
obj.setCountFen(new BigDecimal(ss[3]));
obj.setLockFen(new BigDecimal(ss[4]));
obj.setUseFen(new BigDecimal(ss[5]));
obj.setNotFee(new BigDecimal(ss[6]));
obj.setFlag(ss[7]);
list.add(obj);
map.put(obj.getCustId(), obj);
}
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
//存放360 有,众禄没有的
static List<FundBean> list1 = new ArrayList<FundBean>();
//存放众禄有的,360 没有有
static List<FundBean> list2 = new ArrayList<FundBean>();
//两者都有,不过值不一样的
static List<FundBean> list3 = new ArrayList<FundBean>();
public static void main(String[] args) {
String filePath1 = "C:\\Users\\joey\\Desktop\\test360.txt";
String filePath2 = "C:\\Users\\joey\\Desktop\\test.txt";
List<FundBean> list360 = new ArrayList<FundBean>();
List<FundBean> listZL = new ArrayList<FundBean>();
Map<String,FundBean> map360 = new HashMap<String,FundBean>();
Map<String,FundBean> mapzl = new HashMap<String,FundBean>();
readTxtFile(list360,map360,filePath1);
readTxtFile(listZL,mapzl,filePath2);
System.out.println("fffffffff");
//360有的,众禄没有的
for (int i = 0; i < list360.size(); i++) {
FundBean fub = list360.get(i);
String custId = fub.getCustId();
FundBean zlobject = mapzl.get(custId);
if(zlobject==null){
list1.add(fub);
}else{
//两者都有,并且值不一样的
if(!fub.equals(zlobject)){
list3.add(zlobject);
}
}
}
//众禄有的,360没有的
for (int i = 0; i < listZL.size(); i++) {
FundBean fub = listZL.get(i);
String custId = fub.getCustId();
FundBean obj = map360.get(custId);
if(obj==null){
list2.add(fub);
}
}
//输出到文件
//360有的记录,众禄没有的
String fileName360 = "C:\\Users\\joey\\Desktop\\compareFile\\360.txt";//list1
//众禄有的,360没有的
String fileNameZL = "C:\\Users\\joey\\Desktop\\compareFile\\ZL.txt";//list2
//两者都有,不过值不一样
String fileNameDif = "C:\\Users\\joey\\Desktop\\compareFile\\dif.txt";//list3
StringBuffer stb360 = new StringBuffer();
for (int i = 0; i < list1.size(); i++) {
FundBean be = list1.get(i);
String str = be.toString();
stb360.append(str+"\n");
}
//写入
writeFile(fileName360,stb360.toString());
StringBuffer stbZL = new StringBuffer();
for (int i = 0; i < list2.size(); i++) {
FundBean be = list2.get(i);
String str = be.toString();
stbZL.append(str+"\n");
}
//写入
writeFile(fileNameZL,stbZL.toString());
StringBuffer stbDIF = new StringBuffer();
for (int i = 0; i < list3.size(); i++) {
FundBean be = list3.get(i);
String str = be.toString();
stbDIF.append(str+"\n");
}
//写入
writeFile(fileNameDif,stbDIF.toString());
System.exit(0);
}
/**
* 写入到磁盘中
* @param fileName
* @param fileContent
*/
public static void writeFile(String fileName, String fileContent)
{
try
{
File f = new File(fileName);
if (!f.exists())
{
f.createNewFile();
}
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f),"gbk");
BufferedWriter writer=new BufferedWriter(write);
writer.write(fileContent);
writer.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}