1

I'm using the following code to remove quotemarks from a pipe-delimited CSV:

public Path truncateQuoteMarks(Path path) throws Exception {

    String pipeDelimitedFilename = path.toAbsolutePath().toString();

    Path convertedFilename = Paths.get(pipeDelimitedFilename.substring(0, pipeDelimitedFilename.lastIndexOf(".csv")) + "_no_quotes.csv");

    CSVReader reader = new CSVReader(new InputStreamReader(new BufferedInputStream(new FileInputStream(path.toAbsolutePath().toString()))), '|', CSVParser.DEFAULT_QUOTE_CHARACTER);

    reader.skip(1);

    FileWriter fw = new FileWriter(convertedFilename.toAbsolutePath().toString());
    fw.write(System.lineSeparator());
    CSVWriter writer = new CSVWriter(fw, '|', CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.RFC4180_LINE_END);

    String[] currentLine;
    while((currentLine = reader.readNext()) != null) {
        writer.writeNext(currentLine);
    }

    writer.flush();
    writer.close();
    reader.close();

    return convertedFilename;
}

However, it leaves a blank line at the end of the csv (see picture). enter image description here

Upon googling i found that this has been asked at least once both here and in another site. The other site claims that this is the way OpenCSV works and suggested editing the file size, but there has to be other ways.

Is there a way to remove this blank line from the end of the file? SQL Server BULKINSERT does not like it at all.

Nimchip
  • 1,685
  • 7
  • 25
  • 50

1 Answers1

0

According to RFC4180 the final lineending is optional. So a writer can or can not write it. The openCSVWriter has not option to create a final without line ending. You can extend the Class and add a setter for the line ending and set it empty before writing the last line or write it manual.

For each of this possibilities you should have a look at the source of the writer.

Community
  • 1
  • 1
finder2
  • 842
  • 1
  • 11
  • 30
  • it's not the line ending i'm concerned about, it's the blank line at the end shown by the blue-ish color – Nimchip Nov 07 '18 at 16:53
  • A line is defined by the final line ending. Without this there is no empty line at the end. Try just to remove the line ending and import the file again. – finder2 Nov 07 '18 at 17:08