I have a text file that I use to store information of Items.
10325,Test1,Producto del Hogar,12,17
10425,Test2,Videojuegos,11,17
12345,Test3,Producto del Hogar,56,17.0
I'm using opencsv library to modify one column of the desired item, I'm using this code:
public void updateCSV(String replace, int fila, int col) throws IOException {
if (replace != null) {
File archivoProductos = new File("productos.txt");
// Read existing file
CSVReader reader = new CSVReader(new FileReader(archivoProductos), ',', CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.NO_ESCAPE_CHARACTER);
List<String[]> csvBody = reader.readAll();
// get CSV row column and replace with by using row and column
csvBody.get(fila)[col] = replace;
reader.close();
// Write to CSV file which is open
CSVWriter writer = new CSVWriter(new FileWriter(archivoProductos), ',', CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.NO_ESCAPE_CHARACTER, System.getProperty("line.separator"));
writer.writeAll(csvBody);
writer.flush();
writer.close();
}
}
The problem is that when the modification is done, there is added one empty line at the end of the text file.
line1 10325,Test99,Producto del Hogar,12,17
line3 10425,Test2,Videojuegos,11,17
line2 12345,Test3,Producto del Hogar,56,17.0
line4
How can I avoid the empty line added at the end ?
Added println of csvBody before it writes it out
[10325, Test99, Producto del Hogar, 12, 17]
[10425, Test2, Videojuegos, 11, 17]
[12345, Test3, Producto del Hogar, 56, 17.0]
Tried so far:
Doesn't add the empty line, but my 3 existent lines are now written in just one line.
CSVWriter writer = new CSVWriter(new FileWriter(archivoProductos), ',',
CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.NO_ESCAPE_CHARACTER,"");