![](/img/trans.png)
[英]parsing CSV file in Java with skipping the next row if is the first field is empty
[英]Java - Skipping values in CSV file
我正在嘗試讀取位於以下位置的CSV文件: http : //archive.ics.uci.edu/ml/machine-learning-databases/haberman/haberman.data
我想跳過前兩個值,但是我不太確定該怎么做。
我也想輸入讀取到二維數組中的每一行,但只輸入我真正想要的最后兩個值。 同樣,我不太確定該怎么做,特別是如何根據文件中的行數確定數組的大小,然后將數據實際解析到數組中。
到目前為止,這是我的代碼:
public static void main(String[] args) {
//Input file which needs to be parsed
String fileToParse = "haberman.data";
BufferedReader fileReader = null;
//Delimiter used in CSV file
final String DELIMITER = ",";
try
{
String line = "";
//Create the file reader
fileReader = new BufferedReader(new FileReader(fileToParse));
//Read the file line by line
while ((line = fileReader.readLine()) != null)
{
//
//this is where I get stuck
//
}
}
catch (Exception e) {
e.printStackTrace();
}
finally
{
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
CSV解析起來比您想象的要難。
不要編寫自己的CSV解析代碼,而要使用CSV庫,例如OpenCSV或Commons CSV。 然后,您可以忽略任何您想要的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.