cvs操作类
package src.imp;
import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* CSV操作(导出和导入)
*
* @version 1.0 Jan 27, 2014 4:30:58 PM
*/
public class CSVUtils<T> {
private Class<T> tClass;
private final String filePath;
public CSVUtils(String filePath){this.filePath = filePath;}
/**
* 导出
*
* @param file csv文件(路径+文件名),csv文件不存在会自动创建
* @param dataList 数据
* @return
*/
public boolean exportCsv(File file, List<String> dataList){
boolean isSucess=false;
FileOutputStream out=null;
OutputStreamWriter osw=null;
BufferedWriter bw=null;
try {
out = new FileOutputStream(file);
osw = new OutputStreamWriter(out);
bw =new BufferedWriter(osw);
if(dataList!=null && !dataList.isEmpty()){
for(String data : dataList){
bw.append(data).append("
");
}
}
isSucess=true;
} catch (Exception e) {
isSucess=false;
}finally{
if(bw!=null){
try {
bw.close();
bw=null;
} catch (IOException e) {
e.printStackTrace();
}
}
if(osw!=null){
try {
osw.close();
osw=null;
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
out=null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return isSucess;
}
/**
* 导入
* @param culumNames 字段名(与列对应)
* @return
*/
public List<T> importCvs(String... culumNames){
List<String> dataList=new ArrayList<String>();
File file = new File(filePath);
BufferedReader br=null;
try {
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
dataList.add(line);
}
}catch (Exception e) {
}finally{
if(br!=null){
try {
br.close();
br=null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
List<T> allData = parseListT(dataList, culumNames);
return allData;
}
private List<T> parseListT(List<String> dataList, String[] culumNames) {
List<T> allData = new ArrayList<>();
if(dataList!=null && !dataList.isEmpty()){
for(String data : dataList){
String[] culums = data.split(",");
Map<String, String> oneObjectMap = new HashMap<>();
for (int i = 0; i < culumNames.length; i++) {
oneObjectMap.put(culumNames[i], culums[i]);
}
T t = JSON.parseObject(JSON.toJSONString(oneObjectMap), tClass);
allData.add(t);
}
}
return allData;
}
}
测试类
package src.geym.ch2;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;
import org.junit.Test;
import src.imp.CSVUtils;
import src.imp.User;
/**
* CSV操作(导出和导入)
*
* @version 1.0 Jan 27, 2014 4:17:02 PM
*/
public class CsvTest {
/**
* CSV导出
*
* @throws Exception
*/
@Test
public void importCsv() {
CSVUtils<User> csvUtils = new CSVUtils("/Users/sean/iCloud 云盘(归档)/Desktop/java-web/workspace/prj_src/src/main/resources/ljq.csv");
String[] titile = {"id", "age", "name"};
List<User> users = csvUtils.importCvs( titile);
System.out.println(JSON.toJSONString(users));
}
}
用户
package src.imp;
/**
* TODO
*
* @author sean
* @date 2020/4/2 6:39 PM
*/
public class User{
private String id;
private String age;
private String name;
public User(String id, String age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}