写入包含逗号的CSV [英] Write to CSV that contains comma

查看:198
本文介绍了写入包含逗号的CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做的是将字符串写入CSV文件,问题是一些包含的字符串,将字符串分成一半。所以我试图发送字符串fileInfo下面的CSV,但正如我所说,有时包含一个。是否有人知道如何解决这个问题,因为我是最大的!

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){
    }
  }
}


推荐答案

您可以在值周围更改分隔符或引号。虽然,我建议使用CSV解析器,如 openCSV ,这将为您完成这项工作。

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.

这篇关于写入包含逗号的CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆