FileReader 跳过 CSV 逗号分隔文件中的每一行
FileReader skipping every other line from CSV comma delimitted file
为什么此代码在只有 3 列的 CSV sheet 中每隔一行跳过一次?
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);
// ...
}
为什么此代码在只有 3 列的 CSV sheet 中每隔一行跳过一次?
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);
// ...
}