9

I am trying to input in to the csv file.
My input is as shown below
String string="hi,this is user";
but when i am inputing in to the csv file the comma is taken as delimiter and it is written in two cells. "hi" in one cell and "this is user" in another cell.
But i want the whole string to be in one cell.
How can i achieve this.
Please help me.

FileWriter writer = new FileWriter("D:/Workspace/Sample2.csv");
PrintWriter out = new PrintWriter(writer);

String names="[hasds,jash.jahd,jash]";
    out.append(names);
    out.flush();
}
5
  • Are you using some lib to do this work? Show us the related code to this import process. Commented Apr 19, 2013 at 11:10
  • If you are not using a lib, you could wrap the string in quotes. Not a Java question. Commented Apr 19, 2013 at 11:11
  • FileWriter writer = new FileWriter("D:/Workspace/Sample2.csv"); PrintWriter out = new PrintWriter(writer); String names="hi,this is user"; out.append(names); out.flush(); } Commented Apr 19, 2013 at 11:11
  • @user2243525 Edit your question and post it there. Commented Apr 19, 2013 at 11:13
  • Please use a CSV library for this task. Programmers who have no idea about how a file format works and try to roll their own implementation are responsible for a great number of headaches among other programmers who have to handle the messed-up files these ill-advised attempts have produced. They might even have to resort to using regular expressions (the horror!)... Commented Apr 19, 2013 at 11:40

2 Answers 2

34

Since you're writing a csv file, with comma delimiter, and your text happens to have a comma in it as well, you need to wrap your text within double quotes.

String names = "\"hi,this is user\"";

Note that to wrap your text within double quotes, you need to escape the double quotes as well!

Update:- A sample code snippet to wrap your string within double quotes.

public static void main(String[] args) {

    String test = "abcd";
    System.out.println(test); // prints abcd
    test = appendDQ(test);
    System.out.println(test); // prints "abcd"

}

private static String appendDQ(String str) {
    return "\"" + str + "\"";
}
Sign up to request clarification or add additional context in comments.

7 Comments

u mean, the string has to be appended with doublequotes??
wrapped - begin and end with double quotes(in the string).
check the update! Pass your string to this method & then write it to the file!
but those doublequotes should not be in my string
If the string itself contained double-quotes, wouldn't those need to be escaped as well?
|
0

You have to ensure the data is properly escaped. Commas and double-quotes require special handling. I would highly recommend using a library to take care of all details of properly escaping csv data for you. Take a look at opencsv.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.