James
James

Reputation: 89

Write to CSV that contains comma

What I need to do is write strings to a CSV file, the problem is some string contain a , which will separate the string in half. So I'm trying to send the string "fileInfo" below to the CSV but as I said it sometimes contains a ,. Does any one know how to solve this as I would be most greatful!

public class FileOutput {
  public void FileOutputToFile(String hex) throws Exception{
    String fileInfo = hex;
    try {
      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.csv", true)));
      out.print(fileInfo);
      out.close();
    }catch (IOException e){
    }
  }
}

Upvotes: 2

Views: 6294

Answers (2)

рüффп
рüффп

Reputation: 5438

Another implementation I was happy with: Apache Commons CSV.

The openCSV does also a good job.

Upvotes: 0

Johan Sjöberg
Johan Sjöberg

Reputation: 49177

You can change delimiter or put quotation marks around the values. Although, I recommend using a CSV parser such as openCSV which will do the job for you.

Upvotes: 8

Related Questions