开发者

Write to CSV that contains comma

开发者 https://www.devze.com 2023-02-13 17:59 出处:网络
开发者_如何学运维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 t

开发者_如何学运维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){
    }
  }
}


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.


Another implementation I was happy with: Apache Commons CSV.

The openCSV does also a good job.

0

精彩评论

暂无评论...
验证码 换一张
取 消