Reading CSV file using BufferedReader resulting in reading alternative lines

前端 未结 3 1156
余生分开走
余生分开走 2020-12-21 12:26

I\'m trying to read a csv file from my java code. using the following piece of code:

public void readFile() throws IOException {
    BufferedReader         


        
3条回答
  •  醉话见心
    2020-12-21 12:55

    The first time you're reading the line without processing it in the while loop, then you're reading it again but this time you're processing it. readLine() method reads a line and displaces the reader-pointer to the next line in the file. Hence, every time you use this method, the pointer will be incremented by one pointing to the next line.

    This:

     while ((newLine = br.readLine()) != null) {
            newLine = br.readLine();
            System.out.println(newLine);
            lines.add(newLine);
        }
    

    Should be changed to this:

     while ((newLine = br.readLine()) != null) {
            System.out.println(newLine);
            lines.add(newLine);
        }
    

    Hence reading a line and processing it, without reading another line and then processing.

提交回复
热议问题