在JAVA中另存为选项

时间:2016-02-25 09:58:45

标签: java csv save

我有一个不可读的文件,直到我打开并使用另存为保存。使用SAS代码创建时会出现一些格式问题。在java(自动方式)中是否有“save as”选项,因为输出格式无法调整(来自SAS)。

SAS代码以这种格式生成csv:

"Obs","P10","P20","P30","P40","P50","P60","P70","P80","P90","P91","P92","P93","P94","P95","P96","P97","P98","P99","P100"
"1",.005632791,.007874444,0.010690,0.014358,0.019461,0.027262,0.039370,0.059857,0.10087,0.10837,0.11616,0.12403,0.13478,0.14563,0.15924,0.17487,0.19465,0.22304,0.49123

---文本值采用双引号

当文件使用“保存”重新保存时:

Obs,P10,P20,P30,P40,P50,P60,P70,P80,P90,P91,P92,P93,P94,P95,P96,P97,P98,P99,P100,,,,,,,,,,,,
1,0.005632791,0.007874444,0.01069,0.014358,0.019461,0.027262,0.03937,0.059857,0.10087,0.10837,0.11616,0.12403,0.13478,0.14563,0.15924,0.17487,0.19465,0.22304,0.49123,,,,,,,,,,,,

1 个答案:

答案 0 :(得分:0)

如果您使用OpenCSV撰写CSV文件,则可以将方法CSVWriter.writeNext(String\[\] nextLine, boolean applyQuotesToAll)applyQuotesToAll = true一起使用。

Writer writer = new FileWriter(new File("out.csv"));
CSVWriter csvwriter = new CSVWriter(writer, ',', '"');
String[] line = new String[] {"a", "b", "c"};
csvwriter.writeNext(line, true);
csvwriter.close();

给你

"a","b","c"