只是做一个记录
import java.io.File;
import java.io.FileReader;
import java.util.List;
import au.com.bytecode.opencsv.CSVReader;
public class ReadCSV {
public static void main(String[] args) throws Exception {
File file = new File("e:\\read.csv");
FileReader fReader = new FileReader(file);
CSVReader csvReader = new CSVReader(fReader);
String[] strs = csvReader.readNext();
if(strs != null && strs.length > 0){
for(String str : strs)
if(null != str && !str.equals(""))
System.out.print(str + " , ");
System.out.println("\n---------------");
}
List<String[]> list = csvReader.readAll();
for(String[] ss : list){
for(String s : ss)
if(null != s && !s.equals(""))
System.out.print(s + " , ");
System.out.println();
}
csvReader.close();
}
}
创建对象指定分隔符:
CSVReader csvReader = new CSVReader(fReader, ';');
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import au.com.bytecode.opencsv.CSVWriter;
public class WriteCSV {
public static void main(String[] args) throws Exception {
File file = new File("e:\\write.csv");
Writer writer = new FileWriter(file);
// 指定,分隔符
CSVWriter csvWriter = new CSVWriter(writer, ',');
String[] strs = {"abc" , "abc" , "abc"};
csvWriter.writeNext(strs);
csvWriter.close();
}
}
使用的Jar包链接下载地址为:
<dependency>
<groupId>au.com.bytecode</groupId>
<artifactId>opencsv</artifactId>
<version>2.4</version>
</dependency>
注释:目前该maven地址已经被更新到如下地址
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>4.6</version>
</dependency>
可以去我的博客官网上进行查看!
来源:CSDN
作者:Yarcl
链接:https://blog.csdn.net/u012225679/article/details/102766614