简体   繁体   中英

OpenCSV CSVWriter adds blank line to end of file

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). 在此处输入图片说明

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM