【发布时间】:2017-02-28 14:30:03
【问题描述】:
例如:我正在尝试在 .csv 文件中搜索名称为“abc”的文本,该文件存在于多行的第 6 列中,我需要删除这些行。
我尝试了下面的代码。我能够得到第 6 列中存在文本“abc”的行号/行号,但它没有删除行。
import java.io.BufferedReader;
import java.io.*;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
public class ReadExcel {
public static void main(String[] args) throws Exception{
String csvFile = "csv filelocation";
CSVReader reader = new CSVReader(new FileReader(csvFile));
List<String[]> allElements = reader.readAll();
String [] nextLine;
int lineNumber = 0;
while ((nextLine = reader.readNext()) != null) {
lineNumber++;
if(nextLine[5].equalsIgnoreCase("abc")){
System.out.println("Line # " + lineNumber);
allElements.remove(lineNumber);
}
}
【问题讨论】:
-
你有lines.add(nextLine)的地方,如果你只做lines.split(",")你会得到所有的值在行...那你在做什么拥有它们之后的价值?
-
最好使用适当的库来处理引号和多行值等所有内容,例如Apache Commons CSV。
-
其实我想把所有的单元格存储在一个数组中,然后一个一个地读取。
-
我现在尝试删除行,但无法删除。