I'm trying to remove quote marks from a csv before bulk inserting using an SP (and a FMT file). The problem is our FMT file is counting on a blank line before the headers to process it, so the SP fails if there is no starting blank line. I would like to add a blank line before the headers somehow.
This is my code
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);
//skips headers
reader.skip(1);
CSVWriter writer = new CSVWriter(new FileWriter(convertedFilename.toAbsolutePath().toString()), '|', CSVWriter.NO_QUOTE_CHARACTER);
String[] currentLine;
while((currentLine = reader.readNext()) != null) {
writer.writeNext(currentLine);
}
writer.close();
return convertedFilename;
}
I tried using writer.writeNext(new String[]{});
but it still fails. What am I doing wrong here?
You need to write to the FileWriter directly:
FileWriter fw = new FileWriter(convertedFilename.toAbsolutePath().toString());
fw.write(System.lineSeparator());
CSVWriter writer = new CSVWriter(fw, '|', CSVWriter.NO_QUOTE_CHARACTER);
Then continue as before.