Java - 跳过 CSV 文件中的值
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。然后你可以忽略任何你想要的。
我正在尝试读取位于此处的 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。然后你可以忽略任何你想要的。