How to remove empty line at the end of the file?

雨燕双飞 提交于 2019-12-10 10:19:33

问题


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,"");

回答1:


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.




回答2:


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.




回答3:


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,"");


来源:https://stackoverflow.com/questions/40579601/how-to-remove-empty-line-at-the-end-of-the-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!