Skip first line while reading CSVfile in Java

10,825

Solution 1

Pretty simple:

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

Solution 2

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);
    }

Solution 3

If you didnt write the CSVReader class by yourself then this constructor will skip the first line of the file:

CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', 1);
Share:
10,825
selson
Author by

selson

Updated on June 04, 2022

Comments

  • selson
    selson almost 2 years

    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) {
        ------
    }