数据文件既有卡号又有次数,分别筛选出卡号相同/不同,次数相同/不同的代码实现
package com.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.math.BigInteger;
import org.apache.commons.lang3.StringUtils;
public class KongCompareSingleItem {
public static void main(String[] args) throws Exception {
String wanbo="C:\\wanbo.txt";
String me="C:\\me.txt";
readTxt(wanbo,me);
System.out.println("终于结束了!!!!");
}
private static void readTxt(String wanboPath,String mePath) throws Exception {
BufferedReader wbBr = new BufferedReader(new FileReader(wanboPath));
BufferedReader selfBr = new BufferedReader(new FileReader(mePath));
//写
String meWritePath="C:\\(卡号相同)自己次数大于万博次数.txt";
String wanboWritePath="C:\\(卡号相同)万博次数大于自己次数.txt";
String equalWritePath="C:\\卡号和次数相同.txt";
String meNoCardNumAndwanboHaveCardNumPath="C:\\(卡号)万博有,自己无.txt";
String wanboNoCardNumAndmeHaveCardNumPath="C:\\(卡号)自己有,万博无.txt";
BufferedWriter meWrite=new BufferedWriter(new FileWriter(meWritePath));//(卡号相同)自己次数大于万博次数
BufferedWriter wanboWrite=new BufferedWriter(new FileWriter(wanboWritePath));//(卡号相同)万博次数大于自己次数
BufferedWriter equal=new BufferedWriter(new FileWriter(equalWritePath));//卡号和次数相同
BufferedWriter meHaveButWanboNo = new BufferedWriter(new FileWriter(wanboNoCardNumAndmeHaveCardNumPath));//自己有卡号,万博无卡号路径
BufferedWriter wanboHaveButmeNo = new BufferedWriter(new FileWriter(meNoCardNumAndwanboHaveCardNumPath));//万博有卡号,自己无卡号路径
//wanboHaveButmeNo;meHaveButWanboNo` q1`
//===============================================================================
BigInteger myselfDataCard = null;//自己当前数据卡号
String meLine = selfBr.readLine() ;//自己读一行
String[] splitMeLine = meLine.split(" ");//tab键
String myselfDataCardNum = splitMeLine[0];//自己卡号
String myselfDataTimes = splitMeLine[1];//自己次数
myselfDataCard = new BigInteger(myselfDataCardNum);//自己卡号转为整型
//===============================================================================
BigInteger wbDataCard = null;//万博当前数据卡号
String wbLine = wbBr.readLine();//万博读一行
String[] splitWbLine = wbLine.split(" ");
String wbDataCardNum = splitWbLine[0];//万博卡号
String wbDataTimes = splitWbLine[1];//万博次数
wbDataCard = new BigInteger(wbDataCardNum);//万博卡号转为整型
String myreadNext=null;
String wbReadNext=null;
BigInteger meTempCardNum = new BigInteger("0");//自己卡号临时变量
String meTempCardTimes = null;//自己次数临时变量
BigInteger wbTempCardNum = new BigInteger("0");//万博卡号临时变量
String wbTempCardTimes = null;//万博次数临时变量
//===============================================================================
while(null != myselfDataCard || null != wbDataCard ) {//自己卡号和次数!=null;万博卡号和次数!=null
System.out.println("============================================================");
System.out.println("开始执行循环...");
System.out.println("wbDataCardNum=="+wbDataCardNum+";wbDataTimes=="+wbDataTimes+";myselfDataCardNum"+myselfDataCardNum+";myselfDataTimes=="+myselfDataTimes);
System.out.println("wbTempCardNum=="+wbTempCardNum+";wbTempCardTimes=="+wbTempCardTimes+";meTempCardNum"+meTempCardNum+";meTempCardTimes=="+meTempCardTimes);
if(null != myselfDataCard && null != wbDataCard) {//卡号不为空
System.out.println("进入卡号不为空逻辑");
System.out.println("myselfDataCard=="+myselfDataCard);
System.out.println("myselfDataTimes=="+myselfDataTimes);
System.out.println("wbDataCard=="+wbDataCard);
System.out.println("wbDataTimes=="+wbDataTimes);
if(myselfDataCard.compareTo(wbDataCard) == 0 ) {//自己卡号=万博卡号
System.out.println("进入自己卡号等于万博卡号逻辑");
if (myselfDataTimes.compareTo(wbDataTimes) == 0) {//自己次数=万博次数
meTempCardNum = myselfDataCard;//自己卡号临时变量
meTempCardTimes = myselfDataTimes;//自己次数临时变量
wbTempCardNum = wbDataCard;//万博卡号临时变量
wbTempCardTimes = wbDataTimes;//万博次数临时变量
System.out.println();
System.out.println("进入相同卡号,自己次数等于万博次数逻辑");
equal.write(myselfDataCard+" ");
equal.write('\n');
equal.flush();
myreadNext = selfBr.readLine() ;//自己读下一行
if(StringUtils.isNotBlank(myreadNext) && !"null".equals(myreadNext)) {
System.out.println("=====myreadNext=="+myreadNext);
splitMeLine = myreadNext.split(" ");//tab键
myselfDataCardNum = splitMeLine[0];//自己下一次读的卡号
myselfDataTimes = splitMeLine[1];//自己下一次读的次数
myselfDataCard = new BigInteger(myselfDataCardNum);
}else {
myselfDataCard = null;
myselfDataTimes = null;
}
wbReadNext = wbBr.readLine();//万博读下一行
if(StringUtils.isNotBlank(wbReadNext) && !"null".equals(wbReadNext)) {
System.out.println("========wbReadNext=="+wbReadNext);
splitWbLine = wbReadNext.split(" ");//tab键
wbDataCardNum = splitWbLine[0];//万博读下一次的卡号
wbDataTimes = splitWbLine[1];//万博读下一次的次数
wbDataCard = new BigInteger(wbDataCardNum);
}else {
wbDataCard = null;
wbDataTimes = null;
}
System.out.println("=========================wbDataCard=="+wbDataCard);
} else if(myselfDataTimes.compareTo(wbDataTimes) > 0){//自己次数>万博次数,写入到自己文件中,次数不同,没必要往文件中写入次数(反正要查库)
meTempCardNum = myselfDataCard;//自己卡号临时变量
meTempCardTimes = myselfDataTimes;//自己次数临时变量
wbTempCardNum = wbDataCard;//万博卡号临时变量
wbTempCardTimes = wbDataTimes;//万博次数临时变量
System.out.println("进入相同卡号,自己次数大于万博次数逻辑");
meWrite.write(myselfDataCard+" ");
meWrite.write('\n');
meWrite.flush();
myreadNext = selfBr.readLine() ;//自己读下一行
if(StringUtils.isNotBlank(myreadNext) && !"null".equals(myreadNext)) {
splitMeLine = myreadNext.split(" ");//tab键
myselfDataCardNum = splitMeLine[0];//自己下一次读的卡号
myselfDataTimes = splitMeLine[1];//自己下一次读的次数
myselfDataCard = new BigInteger(myselfDataCardNum);
}else {
myselfDataCard = null;
myselfDataTimes =null;
}
wbReadNext = wbBr.readLine();//万博读下一行
if(StringUtils.isNotBlank(wbReadNext) && !"null".equals(wbReadNext)) {
splitWbLine = wbReadNext.split(" ");//tab键
wbDataCardNum = splitWbLine[0];//万博读下一次的卡号
wbDataTimes = splitWbLine[1];//万博读下一次的次数
wbDataCard = new BigInteger(wbDataCardNum);
}else {
wbDataCard = null;
wbDataTimes = null;
}
}else {//自己次数<万博次数,写入到万博文件中
meTempCardNum = myselfDataCard;//自己卡号临时变量
meTempCardTimes = myselfDataTimes;//自己次数临时变量
wbTempCardNum = wbDataCard;//万博卡号临时变量
wbTempCardTimes = wbDataTimes;//万博次数临时变量
System.out.println("进入相同卡号,自己次数小于万博次数逻辑");
wanboWrite.write(wbDataCard+" ");
wanboWrite.write('\n');
wanboWrite.flush();
myreadNext = selfBr.readLine() ;//自己读下一行
if(StringUtils.isNotBlank(myreadNext) && !"null".equals(myreadNext)) {
splitMeLine = myreadNext.split(" ");//tab键
myselfDataCardNum = splitMeLine[0];//自己下一次读的卡号
myselfDataTimes = splitMeLine[1];//自己下一次读的次数
myselfDataCard = new BigInteger(myselfDataCardNum);
}else {
myselfDataCard = null;
myselfDataTimes = null;
}
wbReadNext = wbBr.readLine();//万博读下一行
if(StringUtils.isNotBlank(wbReadNext) && !"null".equals(wbReadNext)) {
splitWbLine = wbReadNext.split(" ");//tab键
wbDataCardNum = splitWbLine[0];//万博读下一次的卡号
wbDataTimes = splitWbLine[1];//万博读下一次的次数
wbDataCard = new BigInteger(wbDataCardNum);
}else {
wbDataCard = null;
wbDataTimes = null;
}
}
}else if(myselfDataCard.compareTo(wbDataCard) < 0) {//自己卡号<万博卡号
System.out.println("进入自己卡号<万博卡号逻辑");
if (myselfDataCard.compareTo(meTempCardNum) > 0 ) {//自己卡号下一次大于自己卡号上一次
System.out.println("自己卡号<万博卡号,进入自己卡号大于自己上一次卡号逻辑");
meTempCardNum = myselfDataCard;//自己卡号临时变量
meTempCardTimes = myselfDataTimes;//自己次数临时变量
meHaveButWanboNo.write("自己有,万博无:自己卡号小于万博卡号"+myselfDataCard+"");
meHaveButWanboNo.write('\n');
meHaveButWanboNo.flush();
System.out.println("进入自己卡号小于万博卡号逻辑");
System.out.println("wbDataCard=="+wbDataCard);
myreadNext = selfBr.readLine() ;//自己读下一行
if(StringUtils.isNotBlank(myreadNext) && !"null".equals(myreadNext)) {
splitMeLine = myreadNext.split(" ");//tab键
myselfDataCardNum = splitMeLine[0];//自己下一次读的卡号
myselfDataTimes = splitMeLine[1];//自己下一次读的次数
myselfDataCard = new BigInteger(myselfDataCardNum);
}else {
myselfDataCard = null;
myselfDataTimes = null;
}
}
}else if(myselfDataCard.compareTo(wbDataCard) > 0){//自己卡号>万博卡号
System.out.println("进入自己卡号>万博卡号逻辑");
if (wbDataCard.compareTo(wbTempCardNum)>0) {//万博本次卡号大于万博上次卡号
System.out.println("进入万博本次卡号>万博上次卡号逻辑");
wanboHaveButmeNo.write("万博有,自己无:自己卡号大于万博卡号"+wbDataCard);
wanboHaveButmeNo.write('\n');
wanboHaveButmeNo.flush();
wbReadNext = wbBr.readLine();//万博读下一行
if(StringUtils.isNotBlank(wbReadNext) && !"null".equals(wbReadNext)) {
splitWbLine = wbReadNext.split(" ");//tab键
wbDataCardNum = splitWbLine[0];//万博读下一次的卡号
wbDataTimes = splitWbLine[1];//万博读下一次的次数
wbDataCard = new BigInteger(wbDataCardNum);
}else {
wbDataCard = null;
wbDataTimes = null;
}
}
wbTempCardNum = wbDataCard;
wbTempCardTimes = wbDataTimes;
}
}else if(null == wbDataCard && null != myselfDataCard) {
System.out.println("进入自己卡号未读完,万博卡号已读完逻辑");
meHaveButWanboNo.write("进入自己卡号未读完,万博卡号已读完"+myselfDataCard);
meHaveButWanboNo.write('\n');
meHaveButWanboNo.flush();
myreadNext = selfBr.readLine();
if(StringUtils.isNotBlank(myreadNext) && !"null".equals(myreadNext)) {
// meWrite.write("万博已读取完,自己未读取完的卡号"+myreadNext);
// meWrite.write('\n');
// meWrite.flush();
}else {
myselfDataCard = null;
myselfDataTimes = null;
}
}else if(null == myselfDataCard && null != wbDataCard) {
wanboHaveButmeNo.write("自己已读取完,万博未读取完的卡号"+wbDataCard);
wanboHaveButmeNo.write('\n');
wanboHaveButmeNo.flush();
System.out.println("wbDataCard=============++++"+wbDataCard);
wbReadNext = wbBr.readLine();
System.out.println("进入万博卡号未读完,自己卡号已读完逻辑");
System.out.println("wbRead=="+wbReadNext);
if(StringUtils.isNotBlank(wbReadNext) && !"null".equals(wbReadNext)) {
}else {
wbDataCard = null;
wbDataTimes = null;
}
}else {
break;
}
}
equal.close();
meWrite.close();
wanboWrite.close();
wanboHaveButmeNo.close();
meHaveButWanboNo.close();
}
}