示例1
package com.example;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TestCsv {
/**
* 转义字符串中的特殊字符,例如英文逗号
* @param data
* @return
*/
public static String escapeSpecialCharacters(String data) {
String escapedData = data.replaceAll("\\R", " ");
if (data.contains(",") || data.contains("\"") || data.contains("'")) {
data = data.replace("\"", "\"\"");
escapedData = "\"" + data + "\"";
}
return escapedData;
}
/**
* 将字符串数转换为csv格式的一行内容,用英文逗号分隔
* @param data
* @return
*/
public static String convertToCSV(String[] data) {
return Stream.of(data)
.map(TestCsv::escapeSpecialCharacters)
.collect(Collectors.joining(","));
}
/**
* 写 csv 文件
*
* @param filePath
* @param dataLineList
*/
public static void writeCsvFile(String filePath, List<String[]> dataLineList) {
File csvOutputFile = new File(filePath);
try (PrintWriter pw = new PrintWriter(csvOutputFile)) {
dataLineList.stream()
.map(TestCsv::convertToCSV)
.forEach(pw::println);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
List<String[]> dataLineList = new ArrayList<>();
dataLineList.add(new String[]
{ "你好", "带换行\n和带英文逗号," });
dataLineList.add(new String[]
{ "世界", "中国" });
writeCsvFile("/tmp/test.csv", dataLineList);
}
}
运行后,查看csv文件内容:
$ cat /tmp/test.csv
你好,"带换行
和带英文逗号,"
世界,中国
使用 WPS 打开文件:
示例2:指定文件编码
package com.example;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TestCsv {
/**
* 转义字符串中的特殊字符,例如英文逗号
* @param data
* @return
*/
public static String escapeSpecialCharacters(String data) {
String escapedData = data.replaceAll("\\R", " ");
if (data.contains(",") || data.contains("\"") || data.contains("'")) {
data = data.replace("\"", "\"\"");
escapedData = "\"" + data + "\"";
}
return escapedData;
}
/**
* 将字符串数转换为csv格式的一行内容,用英文逗号分隔
* @param data
* @return
*/
public static String convertToCSV(String[] data) {
return Stream.of(data)
.map(TestCsv::escapeSpecialCharacters)
.collect(Collectors.joining(","));
}
/**
* 写 csv 文件
*
* @param filePath
* @param dataLineList
* @param fileEncoding 文件编码,如 GBK
*/
public static void writeCsvFile(String filePath, List<String[]> dataLineList, String fileEncoding) {
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), fileEncoding))) {
for (String[] dataLine : dataLineList) {
String s = convertToCSV(dataLine);
writer.write(s);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
List<String[]> dataLineList = new ArrayList<>();
dataLineList.add(new String[]
{ "你好", "带换行\n和带英文逗号," });
dataLineList.add(new String[]
{ "世界", "中国" });
writeCsvFile("/tmp/test1.csv", dataLineList, "GBK");
}
}
运行后,查看csv文件内容:
$ cat /tmp/test1.csv
���,"����
�ʹ�Ӣ�Ķ���,"
����,�й�
使用 WPS 打开文件不会乱码,因为会自动识别文件编码:
参考
https://www.baeldung.com/java-csv