-2

I tried to search through the web, but not get the answer for my problem. I have a csv file with a line: "Good morning" he said. My program just reads the text from file and print out:

BufferedReader in = new BufferedReader(new FileReader("XXXX/myfile.csv"));
String line;
while ((line = in.readLine()) != null) {
    System.out.println(line);
}

However, the output has more quotation marks than the original:

"""Good morning"" he said."

Can you tell me how to escape those quotation marks so that it prints out as in the file?

DavidNg
  • 2,826
  • 6
  • 32
  • 45
  • Related: http://stackoverflow.com/questions/5738569/splitting-java-string-with-quotation-marks – Caffeinated Oct 06 '12 at 16:31
  • The output seems to be `He said, "Good morning" he said` if we replace `line0` with `line`. – arshajii Oct 06 '12 at 16:32
  • 1
    @Adel: I know that post, I just want to print out as original – DavidNg Oct 06 '12 at 16:34
  • 1
    Your input is not a CSV file and your code has nothing to do with the output you present. Please explain what you are trying to do and what code you actually have. – epsalon Oct 06 '12 at 16:35
  • Can we have all the code btw? – Caffeinated Oct 06 '12 at 16:37
  • What, please do not downvote, I am new to java. What I want is just one quotation mark like in the file – DavidNg Oct 06 '12 at 16:39
  • There's no way that code produces the output you claim, besides that it's not even the same string. How are you viewing the output? How are you running the code? What information are you omitting from your question? – Dave Newton Oct 06 '12 at 16:44
  • Funny, someone keeps voting down. Please answer it professionally – DavidNg Oct 06 '12 at 16:48
  • 1
    It's impossible to answer as it stands, because if the file contains what you claim it does, and the code is as you claim it is, the string will be printed precisely as it exists in the file. – Dave Newton Oct 06 '12 at 16:49
  • Hi Dave, why did you modify to this: He said, "Good morning" he said? – DavidNg Oct 06 '12 at 16:54
  • @DavidNg Hi David, please re-read the edit history, and ask the right person. – Dave Newton Oct 06 '12 at 16:55

2 Answers2

1

You have a CSV file, I doubt your field delimeter for that CSV file is ". This extra quotes will be saved in the CSV file, but wont show up when you open it using Excel or other spreadsheet type program. Check it by opening in a text editor like notepad and see.

prajeesh kumar
  • 1,916
  • 1
  • 17
  • 17
  • You are correct, I copied the CSV file from Windows machine, and then edit in Mac so the delimiters changes somehow, some even are hidden. Thanks a million. – DavidNg Oct 06 '12 at 16:57
0

Easy fix:

str.replaceAll("\\\"{2}", "\"");
Ωmega
  • 42,614
  • 34
  • 134
  • 203