0
entries[6]=artikelmarke;
            entries[7]=artikelkategoriename;
            entries[8]=""+kategorienummer;
            entries[9]=sortierung;

            if(flag)writer.writeNext(entries);
        
            
        }
        writer.close();

I create a CSV like this, but the last Linebreak adds an empty row at the end, which causes trouble in further processing. Anyone know how to avoid that?

enter image description here

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Tolga
  • 262
  • 5
  • 16
  • Are you using opencsv writer? – C.Champagne Jan 27 '14 at 11:33
  • CSVReader reader; //String[] columns = null; HashMap columnNames = new HashMap(); reader = new CSVReader(new FileReader(anbietername+".csv"),';'); CSVWriter writer = new CSVWriter(new FileWriter(anbietername+"parsed.csv"), ';'); – Tolga Jan 27 '14 at 11:33
  • possible duplicate of [Deleting the last line of a file with Java](http://stackoverflow.com/questions/9149648/deleting-the-last-line-of-a-file-with-java). Just remove last symbol. – Mikhail Jan 27 '14 at 11:37
  • Hmm ... my reaction is that the *real* problem here is with your "further processing" and its requirements. According to [RFC 4180](https://www.ietf.org/rfc/rfc4180.txt), a delimiter is required at the end of the last row of a CSV too. `CSVWriter` is simply doing what the spec says it should do. – Stephen C Jan 08 '21 at 01:25

1 Answers1

0

I'm guessing you're using CSVWriter. if you check source code for writeNext, it always adds new line at the end. The easiest solution without changing too much with your project, if files are not too big, is just to write into an in-memory stream, then flush it all into a file except the last new line character.

uiron
  • 890
  • 11
  • 14