package testJava.ari;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class 比较2个文件中每行字段的差异 {
/**
* 找出2个文件中不同的户号 gefei 2013-12-20 15:03:39
*
*/
public static void main(String[] args) {
String encoding = "utf-8";
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
String lineTXT = null;
File file = null;
String f1 = "C:\\Users\\Administrator\\Desktop\\bj\\a1.txt";
String f2 = "C:\\Users\\Administrator\\Desktop\\bj\\a2.txt";
String f3 = "C:\\Users\\Administrator\\Desktop\\bj\\a3.txt";
try{
file = new File(f1);
isr = new InputStreamReader(new FileInputStream(file), encoding);
br = new BufferedReader(isr);
List<String> l1 = new ArrayList<String>();
while ((lineTXT = br.readLine()) != null) {
byte[] bytes = lineTXT.getBytes();
l1.add(new String(bytes));
}
// 找出a1中相同的户号 gefei add 2013-12-21 21:07:09
// for(int i=0;i<l1.size();i++){
// int bs = 0;
// String s1 = l1.get(i);
// for(int j=0;j<l1.size();j++){
// String s2 = l1.get(j);
// if(s1.equals(s2)){
// bs++;
// }
// }
// if(bs > 1){
// System.out.println("l1中重复户号= "+s1);
// }
// }
file = new File(f2);
isr = new InputStreamReader(new FileInputStream(file), encoding);
br = new BufferedReader(isr);
List<String> l2 = new ArrayList<String>();
while ((lineTXT = br.readLine()) != null) {
byte[] bytes = lineTXT.getBytes();
l2.add(new String(bytes));
}
int xtjs = 0;
int bxtjs = 0;
for(int i=0;i<l1.size();i++){
int bz = 0;
String s1 = l1.get(i);
for(int j=0;j<l2.size();j++){
String s2 = l2.get(j);
if(s1.equals(s2)){
xtjs++;
bz = 1;
break;
}
}
if(bz == 0){
bxtjs++;
System.out.println("有差异的内容"+bxtjs+" = "+s1);
}
}
System.out.println("相同的个数 = "+xtjs);
System.out.println("有差异的个数 = "+bxtjs);
// 给a1每行的字符前后加上单引号,后面加上逗号 gefei add 2013-12-30 19:41:35
fos = new FileOutputStream(f3);
for(int i=0;i<l1.size();i++){
String s3 = l1.get(i);
if(i!=l1.size()-1)
s3 = "'"+s3+"',";
else
s3 = "'"+s3+"'";
fos.write(s3.getBytes());
}
System.out.println("-----单引号和逗号增加成功-----");
isr.close();
br.close();
fos.close();
}catch(FileNotFoundException e1){
e1.printStackTrace();
System.exit(-1);
}catch(IOException e2){
e2.printStackTrace();
}
// 打印1到87 gefei add 2013-12-24 11:13:41
// for(int i=1;i<=85;i++){
// System.out.println(i);
// }
}
}