比较Java中具有不同列数(标记)的CSV文件
我已经阅读了其他回答/提问,但没有一个能回答我的问题 我有两个CSV文件,我已经建立了一个程序,通过从比较Java中具有不同列数(标记)的CSV文件,java,csv,Java,Csv,我已经阅读了其他回答/提问,但没有一个能回答我的问题 我有两个CSV文件,我已经建立了一个程序,通过从File1中减去File2中的信息来比较文件,从而将结果保存到File3。目前,我的程序只是将一个只有一列的CSV文件与另一个只有一列的CSV文件(CustomerName)进行比较 这个程序有效 我的问题是:是否有一种方法可以将一个有4列的CSV文件与一个有1列的CSV文件进行比较,并让它减去差异(就像我现在看到的代码),但不只是减去第1列中的差异,而是减去差异及其对应的行 为了更好地解释,
File1
中减去File2
中的信息来比较文件,从而将结果保存到File3
。目前,我的程序只是将一个只有一列的CSV文件与另一个只有一列的CSV文件(CustomerName)进行比较
这个程序有效
我的问题是:是否有一种方法可以将一个有4列的CSV文件与一个有1列的CSV文件进行比较,并让它减去差异(就像我现在看到的代码),但不只是减去第1列中的差异,而是减去差异及其对应的行
为了更好地解释,因为我很难表达我的问题,让我举一个例子: 假设File1.csv包含每个客户
Customer Phone Email Zip
Colt 123456 123@gmail 123456
Mary 321654 123@gmail 123456
John 789654 123@gmail 123456
Trey 987456 123@gmail 123456
假设File2.csv只包含我希望从File1.csv中删除的内容
Customer
Colt
John
我想要的是:
Customer Phone Email Zip
Mary 321654 123@gmail 123456
Trey 987456 123@gmail 123456
因此,只要知道CustomerName,我就希望它不仅删除名称,还删除相应的行
这可能吗
对于任何好奇的人来说,这是我目前拥有的代码,它只适用于1列CSV文件。
包csvdatacompareapplication;
导入java.io.*;
导入java.util.ArrayList;
/*file1-file2=file3*/
公共类CSVDataCompareApplication{
公共静态void main(字符串args[])抛出FileNotFoundException、IOException
{
String path=“C:\\Desktop\\”;
String file1=“customerlistalcustomers.csv”;
String file2=“customerListToRemove.csv”;
String file3=“newCustomerList.csv”;
ArrayList ArrayList 1=新的ArrayList();
ArrayList ArrayList 2=新的ArrayList();
//ArrayList al3=新的ArrayList();
BufferedReader CSVFile1=新的BufferedReader(新文件读取器(路径+文件1));
字符串dataRow1=CSVFile1.readLine();
while(dataRow1!=null)
{
字符串[]dataArray1=dataRow1.split(“,”);
用于(字符串项1:dataArray1)
{
arrayList1.添加(第1项);
}
dataRow1=CSVFile1.readLine();//读取下一行数据。
}
CSVFile1.close();
BufferedReader CSVFile2=新的BufferedReader(新文件读取器(路径+文件2));
字符串dataRow2=CSVFile2.readLine();
while(dataRow2!=null)
{
字符串[]dataArray2=dataRow2.split(“,”);
用于(字符串项2:dataArray2)
{
arrayList2.添加(第2项);
}
dataRow2=CSVFile2.readLine();//读取下一行数据。
}
CSVFile2.close();
for(字符串bs:arrayList2)
{
arrayList1.移除(bs);
}
int size=arrayList1.size();
系统输出打印项次(尺寸);
尝试
{
FileWriter writer=新的FileWriter(路径+文件3);
while(大小!=0)
{
大小--;
writer.append(“+arrayList1.get(size));
writer.append('\n');
}
writer.flush();
writer.close();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
}
package csvdatacompareapplication;
import java.io.*;
import java.util.ArrayList;
/* file1 - file2 = file3*/
public class CSVDataCompareApplication {
public static void main(String args[]) throws FileNotFoundException, IOException
{
String path="C:\\Desktop\\";
String file1="customerListAllCustomers.csv";
String file2="customerListToRemove.csv";
String file3="newCustomerList.csv";
ArrayList<String> arrayList1=new ArrayList<String>();
ArrayList<String> arrayList2=new ArrayList<String>();
//ArrayList al3=new ArrayList();
BufferedReader CSVFile1 = new BufferedReader(new FileReader(path+file1));
String dataRow1 = CSVFile1.readLine();
while (dataRow1 != null)
{
String[] dataArray1 = dataRow1.split(" , ");
for (String item1:dataArray1)
{
arrayList1.add(item1);
}
dataRow1 = CSVFile1.readLine(); // Read next line of data.
}
CSVFile1.close();
BufferedReader CSVFile2 = new BufferedReader(new FileReader(path+file2));
String dataRow2 = CSVFile2.readLine();
while (dataRow2 != null)
{
String[] dataArray2 = dataRow2.split(" , ");
for (String item2:dataArray2)
{
arrayList2.add(item2);
}
dataRow2 = CSVFile2.readLine(); // Read next line of data.
}
CSVFile2.close();
for(String bs:arrayList2)
{
arrayList1.remove(bs);
}
int size=arrayList1.size();
System.out.println(size);
try
{
FileWriter writer=new FileWriter(path+file3);
while(size!=0)
{
size--;
writer.append(""+arrayList1.get(size));
writer.append('\n');
}
writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}