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.