繁体   English   中英

FileReader 从 CSV 逗号分隔文件中每隔一行跳过

[英]FileReader skipping every other line from CSV comma delimitted file

为什么此代码在只有 3 列的 CSV 表中隔行跳过?

public void loadFile(String fileName) {
    try (BufferedReader csvReader = new BufferedReader(new FileReader("C:\\src\\" + fileName + "", StandardCharsets.ISO_8859_1))) {

        while (csvReader.readLine() != null) {

            String[] data = csvReader.readLine().split(",", 3);
            String sku = data[0];
            String title = data[1];
            double price = Double.parseDouble(data[2]);
            Product newProduct = new Product(sku, title, price);
            newProduct.setProducts(newProduct);
        }

    } catch (IOException e) {
        System.out.println("Could not load file: " + e.getMessage());
    }
}

每次调用readLine时,它都会读取一行并移至下一行。 您需要存储读取的行。

String line;
while ((line = csvReader.readLine()) != null) {
    String[] data = line.split(",", 3);
    // ...
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM