Skip first line while reading CSV file in Java

前端 未结 8 2168
陌清茗
陌清茗 2021-02-15 00:30

I am writing a parser code to read a .csv file and parse it to XML. This is the code I have and it works, except I would like it to skip the first line in the file. So I decided

相关标签:
8条回答
  • 2021-02-15 01:11

    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.

    0 讨论(0)
  • 2021-02-15 01:14

    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());
    
    0 讨论(0)
提交回复
热议问题