阅读后,我试图从 CSV 文件中删除第一行。代码工作正常,但它在重写时将“”添加到我的数据中。例如:
- 在写入 csv 文件之前:100,COMRADE,CAMPBELL
写入 csv 文件后:"100","COMRADE","CAMPBELL"
这是代码。
==================================================== =======================
public void deleteLineFromCsv() throws IOException {
List<PatientsTabData> list = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(new File("src/main/resources/patients.csv")));
String line = reader.readLine();
while(line != null) {
String[] split = line.split(",");
list.add(new PatientsTabData().patientId(Integer.parseInt(split[0])).patName(split[1]).patLastName(split[2]));
line = reader.readLine();
}
list.remove(0);
CSVWriter writer = new CSVWriter(new FileWriter(new File("src/main/resources/patients.csv")));
List<String[]> newlist = list.stream().map(item -> new String[]
{String.valueOf(item.getPatientId()), item.getPatName(), item.getPatLastName()})
.collect(Collectors.toList());
writer.writeAll(newlist);
writer.close();
}
图书馆:
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>