0

I'm using Java CSVReader and CSVWriter from OpenCSV

    public static void main(String[] args) throws IOException {
        File inputFile=new File("e:\\temp\\5.csv");
        File outputFile=new File("e:\\temp\\5_out.csv");
        CSVReader reader=new CSVReader(new FileReader(inputFile));
        CSVWriter outputWriter = new CSVWriter(new FileWriter(outputFile),',', '"');
        //CSVWriter outputWriter = new CSVWriter(new FileWriter(outputFile),',', '"','\\');
        Listlines=reader.readAll();

        outputWriter.writeAll(lines);
        outputWriter.flush();
        outputWriter.close();
        reader.close();
    }

input1

"\"7 BURY NEW ROAD  MANCHESTER","","","","Prestwich"

output1

"""7 BURY NEW ROAD  MANCHESTER","","","","Prestwich"

I don't understand why the output is like that. Can someone explain the logic?

If I uncomment the lines where it's commented, basically tells the writer to use '\' to escape quotes and escape characters, it has the following results:

output2

"\"7 BURY NEW ROAD  MANCHESTER","","","","Prestwich"

I also don't understand why the output is like that.

user121196
  • 30,032
  • 57
  • 148
  • 198
  • 1
    That output looks reasonable. What did you expect the output to be like? CSV didn't really have a standard format [until 2005](http://www.ietf.org/rfc/rfc4180.txt) (and even that is just a recommendation, not a rule) and tends to vary a bit between implementations, but traditionally double quotes are escaped with `""`, not `\"`, and RFC4180 specifies `""`. – Jason C Mar 21 '14 at 05:00

1 Answers1

0

I think it makes sense. CSVReader uses \ as escape character and it seems that it can't be changed. Once read, your input starts with the following structure:

DELIMITER - ESCAPED_QUOTATION_MARK

Your first CSVWriter's escape character is ", so the first output starts with

"""

Your second CSVWriter escapes with \, so

"\"

the output is.

(is the disappearing MANCHESTER also a part of the issue or just a typo?)

berbt
  • 252
  • 2
  • 13