关于csv文件,对于新手小白或者大白ex来说都是比较困难的,对于excel文件来说呢,有“easyexcel” 这种工具帮助,但是在写入csv时,使用这个工具不行。
标准的csv文件,其实是一行一行的字符串,每个单元格用 "," 隔开这个是我用记事本写的, “#” 的部分表示的注释,
现在用 wps 打开的话,就是一个表格,用逗号表示单元格,
现在开始上代码,
面对csv文件,我们有两个操作;1.只读 2.写
1.只读(我的读取方式有点儿抽象,大家可以不看)
这个我们可以使用数组,同时也可以使用对象,使用数组的话,我们不需要借助“easyexcel”的帮助,使用对象的话,需要借助一下,我个人建议使用对象
就以我们上面的文件为准
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.3.2</version>
</dependency>
先将这个包引入到项目模块的pom文件中,版本你看你想用什么随意,只要是稳定版就好
创建实体类,因为我用的对象 接收的,所以需要,
import lombok.Data;
@Data
public class User {
private String Id;
private String Name;
private String Sex;
private String Age;
private String Phone;
}
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Component
public class CSVReadAndWrite {
public static void main(String[] args) throws Exception {
read();
}
public static void read() throws IOException {
//1,文件的位置,
String filePath = "F:\\study\\spring\\我爱写算法.csv";
//2.读取文件
// 创建 reader
List<User> UserList = new ArrayList<>();
//我的读取方式是java原生的读取方式 和 使用EasyExcel
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
// 按行读取
String line;
while ((line = br.readLine()) != null) {
// 分割
String[] columns = line.split(",");//因为csv是用”,“分割的
// 打印行
System.out.println("User[" + String.join(", ", columns) + "]");
}
//上面是使用数组的读取方式,
//下面是使用对象的读取方式
//这个是一个使用匿名内部类的读取方式,
EasyExcel.read(filePath, User.class, new ReadListener<User>() {
@Override
public void invoke(User data, AnalysisContext analysisContext) {
UserList.add(data);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}).sheet().headRowNumber(0).doRead();
} catch (IOException ex) {
ex.printStackTrace();
}
//3,打印输出
for (User user : UserList) {
System.out.println("user = " + user.toString());
}
}
}
.headRowNumber(0) 的意思是从 第0行 开始读,就是不识别表头,他会把每一行都读进来。如果你想要它读表头,而且你明确表头是第几行,比如表头是在 第5行,第六行就是数据,那么就是.headRowNumber(6)
2.写
public static void main(String[] args) throws Exception {
//明确知道文件路径
write();
}
public static void write() {
//1.文件的输出路径
String filePath = "F:\\study\\spring";
String fileName = "\\这是我新写的.csv";
String pathName = filePath + fileName;
//检查路径是否存在 这点很重要
try {
Files.createDirectories(Paths.get(filePath));
} catch (IOException e) {
System.out.println("路径创建失败");
throw new RuntimeException(e);
}
File file = new File(pathName);//文件不知道存在不,先新建一个
//准备要写入的文件
ArrayList<User> users = new ArrayList<>();
users.add(new User("#第一行"));
users.add(new User("#第二行"));
users.add(new User("#第三行"));
users.add(new User("#第四行"));
users.add(new User("#第五行"));
users.add(new User("1","迪迦","男","3000万","111"));
users.add(new User("2","戴拿","男","2999万","111"));
users.add(new User("3","盖亚","男","3000万","111"));
users.add(new User("4","塞罗","男","3000万","111"));
users.add(new User("5","爱迪","男","3000万","111"));
users.add(new User("#倒数第五行"));
users.add(new User("#倒数第四行"));
users.add(new User("#倒数第三行"));
users.add(new User("#倒数第二行"));
users.add(new User("#倒数第一行"));
try (BufferedWriter writer = new BufferedWriter(new FileWriter(pathName))) {
for (User user : users) {
String userStr = getStringBuffer(user);
writer.write(userStr);
writer.newLine();
}
System.out.println(filePath + "写入成功");
} catch (Exception e) {
System.out.println(filePath + " 写入失败,出现异常 " + e);
throw new RuntimeException(e);
}
//如果要一次性写入多个csv文件可以这样
/* ArrayList<List<User>> userss = new ArrayList<>();
userss.forEach(users1 -> {
String pathName = "某种获取输入路径的方法";
try (BufferedWriter writer = new BufferedWriter(new FileWriter(pathName))) {
for (User user : users1) {
String userStr = getStringBuffer(user);
writer.write(userStr);
writer.newLine();
}
System.out.println(filePath + "写入成功");
} catch (Exception e) {
System.out.println(filePath + " 写入失败,出现异常 " + e);
throw new RuntimeException(e);
}
});*/
}
private static String getStringBuffer(User user) {
StringBuffer stringBuffer = new StringBuffer();
//判断第一个属性是不是‘#’开头,#开头的内容是注释,只拼开头的第一个内容就好
if (user.getId().trim().contains("#")) {
stringBuffer.append(user.getId());
return stringBuffer.toString();
}
//这里用到了反射,其实本质就是在给他写成一个以‘,’为分割的字符串
Field[] f = User.class.getDeclaredFields();//获取到实体类的所有属性
for (int i = 0; i < f.length; i++) {
//循环获取属性名称
String attributeName = f[i].getName();
//将属性的首字母大写
String methodName = attributeName.substring(0, 1).toUpperCase() + attributeName.substring(1);
try {
//获取属性的get方法
Method getMethod = User.class.getDeclaredMethod("get" + methodName);
//执行该get方法
Object result = getMethod.invoke(user);
if (result != null) {
stringBuffer.append(result).append(",");
} else {
stringBuffer.append(",");
}
} catch (Exception e) {
throw new RuntimeException();
}
}
return stringBuffer.toString();
}
执行结果
如果想要表头就在list集合中按顺序添加就可以了