import lombok.extern.slf4j.Slf4j;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
public class CsvUtil {
/**
* 读取csv文件
* @param fileName
* @param delimiter
* @param isAnsi 是否为windows的ansi格式,就是在windows上新建并用记事本编辑的,可以在“文件--另存为”,对话框最下面看编码
* @return
*/
public static List<List<Object>> getData(String fileName, String delimiter, boolean isAnsi){
List<List<Object>> csvData = new ArrayList<>();
try {
Path filePath = Paths.get(fileName);
Charset fileCharset;
if (!isAnsi){
fileCharset = StandardCharsets.UTF_8;
}else{
fileCharset = Charset.forName("CP1252");
}
Files.lines(filePath, fileCharset).forEach(s->{
if(s == null || s.length() ==0){
csvData.add(null);
return;
}
List<Object> lineData = new ArrayList<>();
csvData.add(lineData);
if (isAnsi){
try {
s =new String(s.getBytes("CP1252"), "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
// 保留空
for(String one:s.split(delimiter,-1)){
if (one == null || one.length() == 0){
lineData.add(null);
}else{
lineData.add(one);
}
}
});
} catch (IOException e) {
log.error("error file:{}",fileName);
e.printStackTrace();
}
return csvData;
}
public static String toCsv(List<List<Object>> csvData, String delimiter){
StringBuilder sb = new StringBuilder();
for(List<Object> rowData:csvData){
if (rowData == null){
sb.append(System.lineSeparator());
continue;
}
List<String> oneRow = new ArrayList<>();
for(Object obj:rowData){
if (obj == null || (obj instanceof String && ((String) obj).length() == 0)){
oneRow.add(null);
continue;
}
if (obj instanceof String){
oneRow.add((String) obj);
}else if(obj instanceof Integer || obj instanceof Double || obj instanceof Long){
oneRow.add(String.valueOf(obj));
}else{
oneRow.add(obj.toString());
}
}
String rowStr = oneRow.stream()
.map(e->e == null ? "" : e)
.collect(Collectors.joining(delimiter));
sb.append(rowStr).append(System.lineSeparator());
}
return sb.toString();
}
public static byte[] toAnsi(String content){
byte[] data = null;
if (content == null || content.length() == 0)
return null;
try {
data = content.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return data;
}
public static void toFile(String fileName, byte[] data){
if (data == null || data.length == 0)
return;
try {
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(data);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试用例
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class CsvUtilTest {
@Test
void getData() {
List<List<Object>> csvData = CsvUtil.getData("one.txt",",",true);
System.out.println(csvData);
}
@Test
void toCsv() {
List<List<Object>> csvData = CsvUtil.getData("one.txt",",",true);
String csvStr = CsvUtil.toCsv(csvData,",");
// System.out.println(csvStr);
CsvUtil.toFile("test.txt",csvStr.getBytes());
CsvUtil.toFile("test2.txt",CsvUtil.toAnsi(csvStr));
}
}