我在 .csv 文件中有如下数据。
| Name | Age | Place|
|Ram | 20 | AP |
|Srinu | 25 | TS |
|Raju | 22 | TN |
如何仅读取 csv 文件上方的 Place 并使用列表存储所有值
答案 0 :(得分:0)
试试下面的:
BufferedReader br = new BufferedReader(new FileReader("/Path/To/file.csv"));
String line = "";
String DELIMITER = "|";
List<String> records = new ArrayList<>();
while ((line = br.readLine()) != null) {
String[] values = line.split(DELIMITER);
records.add(values[2]);
}
答案 1 :(得分:0)
您可以使用 CSVParser 类:
CSVParser csvParser = new CSVParserBuilder().withSeparator('|').build();
try (CSVReader reader = new CSVReaderBuilder(new FileReader("/Path/To/file.csv"))
.withCSVParser(csvParser) // custom CSV parser
.build()) {
List<String[]> r = reader.readAll();
} catch (IOException | CsvException e) {
e.printStackTrace();
}
查看更多关于使用 CSVParser here