使用java中的openCSV读取CSV文件并写入新的CSV文件
我正在编写一个程序,读取CSV文件,并修改一些列。修改后,我必须将其写入一个新的CSV文件进行修改
我一直在读和修改,但当我把它写入一个新文件时,我只得到一行。我被打动了
帮我克服这个
我使用的是openCSV,语言是Java
我的代码:
CSVReader reader;
try
{
File file=new File("/home/srinivas/Desktop/saicharan/Cardsmod.csv");
if(file.createNewFile()){ }
reader = new CSVReader(new FileReader(filename));
String filewrite="/home/srinivas/Desktop/saicharan/Cardsmod.csv";
CSVWriter writer=new CSVWriter(new FileWriter(filewrite));
String[] row;
while((row = reader.readNext()) != null)
{
String str=new String();
String str1=new String();
for(int i=0;i<row.length;i++)
{
str1=str1+","+row[i];
if(row[i].equals("Card Text Listen"))
{
String [] nextLine = reader.readNext();
String [] nextLine1=reader.readNext();
str=str+nextLine[i]+nextLine1[i];
c2.met(str);
}
if(i==11)
{
String[] rowwrite=str1.split(",");
writer.writeNext(rowwrite);
}
}
}
writer.close();
}
谢谢
# 1 楼答案
我看到您读写同一个文件,所以您应该将所有行存储到一个列表中,然后在读取器完成后初始化writer 经验: