Skip first line while reading CSV file in Java

I am rather confused by your code, your have the lineMap and you also have fw (whatever that is). Which one are you using? You say you want to skip the first line, but you don't

if (firstLine == true) {
   firstLine = false;
   continue;
}

I would also suggest using a library like CSVReader which I belive even has a property ignoreFirstLine

http://opencsv.sourceforge.net/apidocs/au/com/bytecode/opencsv/CSVReader.html


Create a variable interation and initialize with 0. Check it as very first thing in while loop.

String line;
int iteration = 0;
while ((line = br.readLine()) != null) {
    if(iteration == 0) {
        iteration++;  
        continue;
    }
    ...
    ...
}

I feel compelled to add a java 8 flavored answer.

List<String> xmlLines = new BufferedReader(new FileReader(csvFile))
    .lines()
    .skip(1) //Skips the first n lines, in this case 1      
    .map(s -> {
        //csv line parsing and xml logic here
        //...
        return xmlString;
    })
    .collect(Collectors.toList());

You might consider placing headerLine = br.readLine() before your while loop so you consume the header separately from the rest of the file. Also you might consider using opencsv for csv parsing as it may simplify your logic.

Tags:

Java

Csv

Line

Skip