I'm trying to remove fist line from the CSV file after reading it. Code is working properly but it's adding "" to my data when it's rewriting. for an example:
- before write to csv file: 100,COMRADE,CAMPBELL
after write to csv file: "100","COMRADE","CAMPBELL"
Here is the code.
========================================================================
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();
}
Library:
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>
You can use another constructor when creating your writer. Change
to