简体   繁体   English

csvReader正在从csv文件读取时跳过第一列每一行的第一个字母

[英]csvReader is skipping the first alphabet of every row of fist column while reading from a csv file

I wrote a csvReader method in Java. 我用Java编写了一个csvReader方法。 Everything seems to work fine, but it is just skipping the very first alphabet of every row in first column while printing the values. 一切似乎都可以正常工作,但是在打印值时只是跳过了第一列中每一行的第一个字母。 Here is my code => 这是我的代码=>

public static void csvReader(String fileName) {

    try {
        BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
        List<Student> students = new ArrayList<>();
        fileReader.readLine();

        while (fileReader.read() > 0) {
            String line = fileReader.readLine();
            String[] tokens = line.split(COMMA_DELIMETER);

            if (tokens.length > 0) {
                Student student = new Student(tokens[0], tokens[1], Integer.valueOf(tokens[2]),
                        Integer.valueOf(tokens[3]));
                students.add(student);
            }
        }

        students.forEach(System.out::println);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Here "Student" is just a POJO class having firstName, lastName, id and age fields. 在这里,“学生”只是一个具有firstName,lastName,id和age字段的POJO类。 In am getting the output as => 在我得到的输出为=>

Student details : ID = 101 first name = ikhil Last name = Chaurasia age = 28
Student details : ID = 102 first name = adhna Last name = Chaurasia age = 28

Where the result should be like => 结果应该像=>

Student details : ID = 101 first name = Nikhil Last name = Chaurasia age = 28
Student details : ID = 102 first name = Sadhna Last name = Chaurasia age = 28

The csv file content are shown below : csv文件的内容如下所示:

csv_file.csv

The toString method is implemented as shown below : toString方法的实现如下所示:

public String toString() {
    return "Student details : ID = "+id+ " first name = "+firstName+ " Last name = "+lastName+ " age = "+age;
}

Any help would be highly appreciated. 任何帮助将不胜感激。 Thanks 谢谢

The issue is in your while loop condition: fileReader.read() > 0 . 问题出在您的while循环条件中: fileReader.read() > 0

This reads a single character every time you call it (such as 'N', 'S'), which causes the subsequent call to readLine to skip the first character. 每次调用该字符时都会读取一个字符(例如“ N”,“ S”),这将导致随后对readLine调用会跳过第一个字符。

Instead, just check the condition using readLine : 相反,只需使用readLine检查条件:

String line;
while ((line = fileReader.readLine()) != null) {
  String[] tokens = line.split(COMMA_DELIMETER);
  // do the rest of the stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用java中的CsvReader从csv文件中读取特定行 - how to read a specific row from csv file using CsvReader in java 使用openCSV CSVReader读取csv文件时出现Java应用程序错误... java.lang.ArrayIndexOutOfBoundsException - Java application Error while reading a csv file using openCSV CSVReader … java.lang.ArrayIndexOutOfBoundsException 在JAVA 6中使用CSVReader(OpenCSV)读取CSV时避免ArrayIndexOutOfBoundsException - Avoiding ArrayIndexOutOfBoundsException while reading a CSV using CSVReader(OpenCSV) in JAVA 6 如果第一个字段为空,则通过跳过下一行来解析Java中的CSV文件 - parsing CSV file in Java with skipping the next row if is the first field is empty 从CSV读取并分配给不同类型时,BufferedReader会跳过字段 - BufferedReader skipping fields while reading from CSV and assigning to different types FileReader 从 CSV 逗号分隔文件中每隔一行跳过 - FileReader skipping every other line from CSV comma delimitted file 有关从文件读取时跳过替换行的说明? - Explanation regarding skipping of alternate lines while reading from a file? 从 csv 文件读取第一个元素时出错 - Error in reading first element from csv file 读取.tsv文件时跳过备用行 - Skipping alternate lines while reading .tsv file VB在读取文本文件时跳过行 - VB skipping lines while reading in text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM