3

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,"");
David Selem
  • 121
  • 2
  • 15
  • what does csvBody look like before you write it out? you can use print statements to see. – matias elgart Nov 13 '16 at 22:41
  • Can you try with this ? CSVWriter writer = new CSVWriter(new FileWriter(archivoProductos), ',', CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER); – Vasu Nov 13 '16 at 22:49
  • I updated the post, as you can see there is no empty line in the csvBody, If I delete the empty line and want to modify another row, the empty line will be back. @melgart – David Selem Nov 13 '16 at 22:50
  • I've already tried that, didn't work :( @javaguy – David Selem Nov 13 '16 at 22:51

3 Answers3

0

Perhaps you would like to insert a test so you don't write the final...

System.getProperty("line.separator")

...if there is no more data. Or, and this may execute faster, just trim the last separator character before writing/saving the file.

0

The problem is your line feed character, so instead of System.getProperty("line.separator"), you need to send the line feed (last argument) as "" for the CSVWriter constructor as shown below:

//Pass Empty string as line feed at the end
CSVWriter writer = new CSVWriter(new FileWriter(archivoProductos), ',', 
                  CSVWriter.NO_QUOTE_CHARACTER,
                  CSVWriter.NO_ESCAPE_CHARACTER,"");
Vasu
  • 21,832
  • 11
  • 51
  • 67
0

Okay so looking at it from a slightly different angle I would ask why are you sending in the System.getProperty("line.separator") - and the answer to that is that the system you are on is uses a line separator other than newline "\n".

What I am suspecting is that the editor you are using is not handling the different newline well. If you are on a *Nix system (Linux, Unix, BSD, Mac, etc) run "wc -l " to get a real line count and see if that comes back with a three or four. Otherwise try a different editor.

Another possibility is do a hexdump of you input file and see if it has an extra newline in it. It could be that is the case and the reader is picking that up as an extra, empty, record and the CSVWriter is dutifully writing it out. If so you will need to write logic to loop through the List after it is read and remove the ones that are empty before writing. I think this is the real culprit here.

Scott Conway
  • 975
  • 7
  • 13