简体   繁体   中英

Skip first line while reading CSVfile in Java

While reading a CSV file, I need to ignore the first line. First row has date and heading of the CSV which I don't need to read.

I have to read only from the second row onward. Can anyone please help me?

String csvFilename = "C:\\Data\\csv_files\\REPORT.csv";
String filterCSV = "C:\\Data\\csv_files\\Output.csv";
CSVWriter write = new CSVWriter(new FileWriter(filterCSV));
CSVReader csvR = new CSVReader(new FileReader(csvFilename)); ---- 
List<CSVData> list = csv.parse(col, csvR);

for (Object object : list) {
    ------
}

Pretty simple:

 List<CSVData> list = csv.parse(col, csvR);
 list.remove(0);

如果您没有自己编写 CSVReader 类,那么此构造函数将跳过文件的第一行:

CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', 1);

Before reading the entire data in a while loop using BufferedReader use br.readLine() then first line of csv file will be eliminated, then read the remaining contents using readLine() function.

    BufferedReader br = new BufferedReader(new FileReader("file.csv"));
    String line = " ";
    br.readLine();
    while((line=br.readLine())!=null)
    {
        System.out.println(line);
    }

You can also use the methods in CSVParser.java

BufferedReader br = new BufferedReader(new FileReader("file.csv"));
CSVParser csvParser = new CSVParser(reader, 
CSVFormat.DEFAULT.withHeader("firstColHeader","secondColHeader").withFirstRecordAsHeader().withTrim());

首先,您必须像ArrayList一样添加到List ,然后像这样删除index[0]

list.remove(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM