skipping lines while reading from csv file in java [duplicate]

a 夏天 提交于 2021-02-11 15:28:56

问题


private static List<Book> readDataFromCSV(String fileName) {    
    List<Book> books = new ArrayList<>();
    Path pathToFile = Paths.get(fileName);

    // create an instance of BufferedReader
    // using try with resource, Java 7 feature to close resources
    try (BufferedReader br = Files.newBufferedReader(pathToFile,
            StandardCharsets.US_ASCII)) {
        // read the first line from the text file
        String line = br.readLine();

        // loop until all lines are read
        while ((line  = br.readLine())!= null) {
            // use string.split to load a string array with the values from
            // each line of
            // the file, using a comma as the delimiter
            String[] attributes = line.split("\\|");

            Book book = createBook(attributes);

            // adding book into ArrayList
            books.add(book);

            // read next line before looping
            // if end of file reached, line would be null
            line = br.readLine();
        }

    } catch (IOException ioe) {
        ioe.printStackTrace();
    } 
    return books;
}


private static Book createBook(String[] metadata) { 
    String name = metadata[0];  
    String author = metadata[1]; // create and return book of this metadata 
    return new Book(name, price, author); 
} 

The above code skips every second line from text file (a csv file). It gives data of alternate lines and it uses Java 7 syntax. Please provide some suggestion what is wrong or how to improve it.


回答1:


Remove the br.readLine() inside the while condition i.e.

// read the first line from the text file
String line = br.readLine();
// loop until all lines are read
while (line != null)
{
    ...
    // read next line before looping
    // if end of file reached, line would be null
    line = br.readLine();
}



回答2:


You have called the br.readLine() function twice in the loop.

One is in the condition:

while((line = br.readLine()) != null)

and the second one is at end of the loop.

So the loop is actually reading a line at the end, and then reading the next line at the beginning without processing it. To avoid this, you can remove the br.readLine at the end of the loop.

while ((line  = br.readLine())!= null)
                {

                    // use string.split to load a string array with the values from
                    // each line of
                    // the file, using a comma as the delimiter
                   String[] attributes = line.split("\\|");

                   Book book = createBook(attributes);

                   // adding book into ArrayList
                   books.add(book);
               }

If you did not get it, the condition:

while((line = br.readLine()) != null)

is actually doing the following:

storing the returned value of br.readLine() in the variable line,

and then checking the condition. Therefore, you do not need to call it again in the loop.



来源:https://stackoverflow.com/questions/59138365/skipping-lines-while-reading-from-csv-file-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!