1

I try to generate a CSV file, the data is separated with a comma ",", and new line with "\n". The new line is working, but not the comma, I have all the data in the first column, not in two columns as I wish.

Here is the code:

BufferedWriter writer = new BufferedWriter(new FileWriter("test.csv"));
writer.write("Username");
writer.write(",");
writer.write("Count");
writer.write("\n");
writer.write("Bob");
writer.write(",");
writer.write("20");
writer.write("\n");
writer.write("Mike");
writer.write(",");
writer.write("32");
writer.write("\n");
writer.close();

The result is this:

Username,Count
Bob,20
Mike,32

The expected result is something like that

Username | Count
  ------ | ------
    Bob  |  20
  ------ | ------
    Mike |  32
7
  • 1
    I don't see your problem. You have your expected result, do you not? Commented Jul 22, 2016 at 13:51
  • 1
    It works if I use ; as a separator in a txt file then change the extension, not with ,... don't ask why Commented Jul 22, 2016 at 13:51
  • 2
    If you need result as second one then definately you need to work to get that design, Yes the second one is not a csv file format. Commented Jul 22, 2016 at 13:52
  • @AxelH could you define "works"? Commented Jul 22, 2016 at 13:53
  • 1
    In that case alternative solution could be setting separator in firs line with sep=, as mentioned in superuser.com/a/686415/371154. But still we can't be sure that this is what OP wants until OP will clarify his problem. Commented Jul 22, 2016 at 13:56

3 Answers 3

1

On a project here, we create CSV with ; separator to create our file instead of ,.

This is correctly parse with Excel without doing any changes in the file or in Excel.

EDIT : As Pshemo said, you can specify the separator in the first line of you file. Like this :

sep=,
a,b
c,d

This will be correctly formatted in Excel (Tested). I don't use this solution because I use my generated file for archive and transfer of data so I don't want to check if the first line is data or just properties. But this is a more general solution.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want your file to look like this:

Username | Count
  ------ | ------
    Bob  |  20
  ------ | ------
    Mike |  32

then you need to modify your code to write that. But, you're getting exactly the expected results

Username,Count
Bob,20
Mike,32

with CSV (Comma-separated Values) which, as it says, is data separated by commas and each line being a register. I don't know what you expected posting this question.

6 Comments

This does not really solve the asker's question. Since you are saying his questions is misphrased, it is more suited as a comment.
I don't have enough reputation to post a comment, you should notice.
Hardly a reason to post as an answer when it should have been a comment. I suggest you take a look at the answering guidelines stackoverflow.com/help/how-to-answer.
Answers which state that "it works fine/it works for me" in most cases are nothing more than comments suggesting that OP asked wrong question, or didn't describe his problem properly. Such comment already exists so there is no point in reposting it as answer (more info: Are “works for me” answers valid?).
Yes he did not describe his problem properly. My point is, i would have added a comment if i could, but i can't because i don't have enough reputation. Should we close this topic? -_-
|
0

I believe the file is being created correctly, that is how csv files work. When opening them in excel for example, go to the Data Header, then click Text to Columns, next select Delimited in the window that pops up, then after hitting next, select comma, then hit finish!

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.