如何写入带有多个逗号的CSV文件?

时间:2019-03-19 09:48:43

标签: java csv

这是我写文件的代码:

public void CreateCSV(PerfomanceBST[] temp) throws IOException {
    try (PrintWriter writer = new PrintWriter("InstrumentationCSV.csv", "UTF-8")) {
        String out = "";
        String lineSep = System.getProperty("line.separator");
        for (PerfomanceBST each : temp) {
           out += each.MAX + "; " + each.MIN + "; " + each.getAverage() + "\r\n";
            //writer.write(out + "\r\n");
        }
        writer.print(out);
    }
}

它将3个值生成为一列。

1 个答案:

答案 0 :(得分:-1)

使用以下代码,它将创建逗号分隔的CSV

public static void CreateCSV(){
        try (PrintWriter writer = new PrintWriter("/tmp/InstrumentationCSV.csv", "UTF-8")) {
            StringBuilder out = new StringBuilder();
            String lineSep = System.getProperty("line.separator");
             for (PerfomanceBST each : temp) {
               out.append(each.MAX + ", " + each.MIN + ", " + each.getAverage() + "\r\n");
            }
            writer.print(out.toString());
        } catch (Exception e)  {

        }
    }