逗号分隔值 (CSV) 文件只是一个普通的纯文本文件,将数据逐列存储,并通过分隔符将其拆分(例如,通常它是逗号“,”)。
OpenCSV 是Java的 CSV 解析器库。 OpenCSV 支持您想要执行的所有基本 CSV 类型操作。 Java 7 目前是 OpenCSV 支持的最低版本。 Java语言不提供有效处理 CSV 文件的任何本机支持,因此我们使用 OpenCSV 来处理Java的CSV 文件。
如何使用 OpenCSV
1. 对于 maven 项目,您可以在 pom.xml 文件中包含 OpenCSV maven 依赖项。
HTML
com.opencsv
opencsv
4.1
Java
// Java code to illustrate reading a
// CSV file line by line
public static void readDataLineByLine(String file)
{
try {
// Create an object of filereader
// class with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvReader object passing
// file reader as a parameter
CSVReader csvReader = new CSVReader(filereader);
String[] nextRecord;
// we are going to read data line by line
while ((nextRecord = csvReader.readNext()) != null) {
for (String cell : nextRecord) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
Java
// Java code to illustrate reading a
// all data at once
public static void readAllDataAtOnce(String file)
{
try {
// Create an object of file reader
// class with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvReader object and skip first Line
CSVReader csvReader = new CSVReaderBuilder(filereader)
.withSkipLines(1)
.build();
List allData = csvReader.readAll();
// print Data
for (String[] row : allData) {
for (String cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
Java
// Java code to illustrate
// Reading CSV File with different separator
public static void readDataFromCustomSeparator(String file)
{
try {
// Create an object of file reader class with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvParser object with
// custom separator semi-colon
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
// create csvReader object with parameter
// filereader and parser
CSVReader csvReader = new CSVReaderBuilder(filereader)
.withCSVParser(parser)
.build();
// Read all data at once
List allData = csvReader.readAll();
// Print Data.
for (String[] row : allData) {
for (String cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
Java
// Java program to illustrate reading
// two CSV files
// with different separators
import java.io.FileReader;
import java.util.List;
import com.opencsv.*;
public class ReadCSVData {
private static final String CSV_FILE_PATH
= "D:\\EclipseWorkSpace\\CSVOperations\\results.csv";
private static final String CSV_FILE_CUSTOM_SEPARATOR
= "D:\\EclipseWorkSpace\\CSVOperations\\results_semicolon_Separator.csv";
public static void main(String[] args)
{
System.out.println("Read Data Line by Line With Header \n");
readDataLineByLine(CSV_FILE_PATH);
System.out.println("_______________________________________________");
System.out.println("Read All Data at Once and Hide the Header also \n");
readAllDataAtOnce(CSV_FILE_PATH);
System.out.println("_______________________________________________");
System.out.println("Custom Separator here semi-colon\n");
readDataFromCustomSeparator(CSV_FILE_CUSTOM_SEPARATOR);
System.out.println("_______________________________________________");
}
public static void readDataLineByLine(String file)
{
try {
// Create an object of filereader class
// with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvReader object passing
// filereader as parameter
CSVReader csvReader = new CSVReader(filereader);
String[] nextRecord;
// we are going to read data line by line
while ((nextRecord = csvReader.readNext()) != null) {
for (String cell : nextRecord) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void readAllDataAtOnce(String file)
{
try {
// Create an object of filereader class
// with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvReader object
// and skip first Line
CSVReader csvReader = new CSVReaderBuilder(filereader)
.withSkipLines(1)
.build();
List allData = csvReader.readAll();
// print Data
for (String[] row : allData) {
for (String cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void readDataFromCustomSeparator(String file)
{
try {
// Create object of filereader
// class with csv file as parameter.
FileReader filereader = new FileReader(file);
// create csvParser object with
// custom separator semi-colon
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
// create csvReader object with
// parameter filereader and parser
CSVReader csvReader = new CSVReaderBuilder(filereader)
.withCSVParser(parser)
.build();
// Read all data at once
List allData = csvReader.readAll();
// print Data
for (String[] row : allData) {
for (String cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
2. 对于 Gradle 项目,您可以包含 OpenCSV 依赖项。
compile group: 'com.opencsv', name: 'opencsv', version: '4.1'
3. 您可以下载 OpenCSV Jar 并将其包含在您的项目类路径中。
一些有用的 opencsv 类
- CSVReader –此类提供读取 CSV 文件作为字符串数组列表的操作。
- CSVWriter –此类允许我们将数据写入 CSV 文件。
- CsvToBean –当您想从 CSV 文件内容填充Java bean 时,将使用此类。
- BeanToCsv –此类有助于将数据从Java应用程序导出到 CSV 文件。
读取 CSV 文件
要读取 CSV 文件,您需要 CSVReader 类。以下是我们将阅读的示例 CSV 文件。
name, rollno, department, result, cgpa
amar, 42, cse, pass, 8.6
rohini, 21, ece, fail, 3.2
aman, 23, cse, pass, 8.9
rahul, 45, ee, fail, 4.6
pratik, 65, cse, pass, 7.2
raunak, 23, me, pass, 9.1
suvam, 68, me, pass, 8.2
我们可以通过两种方式读取 csv 文件:
1. 逐行读取数据:让我们看看如何逐行读取 CSV 文件。为了逐行读取数据,首先我们必须通过传递 CSV 文件的 filereader 对象来构造和初始化 CSVReader 对象。之后我们必须调用 CSVReader 对象的 readNext() 方法来逐行读取数据,如下面的代码所示。
Java
// Java code to illustrate reading a
// CSV file line by line
public static void readDataLineByLine(String file)
{
try {
// Create an object of filereader
// class with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvReader object passing
// file reader as a parameter
CSVReader csvReader = new CSVReader(filereader);
String[] nextRecord;
// we are going to read data line by line
while ((nextRecord = csvReader.readNext()) != null) {
for (String cell : nextRecord) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
2. 一次性读取所有数据:我们使用 readNext() 方法一一读取 CSV 记录。 CSVReader 还提供了一个名为 readAll() 的方法来一次将所有记录读入一个 List 中。
List allData = csvReader.readAll();
当我们默认读取 csv 文件时,header 不会被忽略,如上面代码的输出所示。当我们需要跳过列表中的第一个元素时,我们可以在创建 CSVReader 时指定开始行。
CSVReader csvReader =
new CSVReaderBuilder(reader).withSkipLines(1).build();
代码:
Java
// Java code to illustrate reading a
// all data at once
public static void readAllDataAtOnce(String file)
{
try {
// Create an object of file reader
// class with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvReader object and skip first Line
CSVReader csvReader = new CSVReaderBuilder(filereader)
.withSkipLines(1)
.build();
List allData = csvReader.readAll();
// print Data
for (String[] row : allData) {
for (String cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
使用不同的分隔符读取 CSV 文件
CSV 文件可以用逗号以外的分隔符分隔,例如分号、管道等。以下示例显示如何读取由分号字符分隔的 CSV 文件的数据。
分号分隔的 CSV 文件示例:
name;rollno;department;result;cgpa
amar;42;cse;pass;8.6
rohini;21;ece;fail;3.2
aman;23;cse;pass;8.9
rahul;45;ee;fail;4.6
pratik;65;cse;pass;7.2
raunak;23;me;pass;9.1
suvam;68;me;pass;8.2
对于自定义分隔符,首先创建具有特定解析器字符的CSVParser。
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
然后我们将使用 CSVParser() 方法和构造函数创建 CSVReader 对象,并将生成的解析器对象提供给 withCSVParser 方法的参数。最后调用 build 方法来构建对象。
CSVReader csvReader = new CSVReaderBuilder(filereader).withCSVParser(parser).build();
代码:
Java
// Java code to illustrate
// Reading CSV File with different separator
public static void readDataFromCustomSeparator(String file)
{
try {
// Create an object of file reader class with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvParser object with
// custom separator semi-colon
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
// create csvReader object with parameter
// filereader and parser
CSVReader csvReader = new CSVReaderBuilder(filereader)
.withCSVParser(parser)
.build();
// Read all data at once
List allData = csvReader.readAll();
// Print Data.
for (String[] row : allData) {
for (String cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
示例 – 读取两个 csv 文件 result.csv 和 results_semicolon_Separator.csv
result.csv 有默认分隔符 ‘, ‘ 但 results_semicolon_Separator.csv 有一个分隔符 ‘;’代替’,’。
代码:
Java
// Java program to illustrate reading
// two CSV files
// with different separators
import java.io.FileReader;
import java.util.List;
import com.opencsv.*;
public class ReadCSVData {
private static final String CSV_FILE_PATH
= "D:\\EclipseWorkSpace\\CSVOperations\\results.csv";
private static final String CSV_FILE_CUSTOM_SEPARATOR
= "D:\\EclipseWorkSpace\\CSVOperations\\results_semicolon_Separator.csv";
public static void main(String[] args)
{
System.out.println("Read Data Line by Line With Header \n");
readDataLineByLine(CSV_FILE_PATH);
System.out.println("_______________________________________________");
System.out.println("Read All Data at Once and Hide the Header also \n");
readAllDataAtOnce(CSV_FILE_PATH);
System.out.println("_______________________________________________");
System.out.println("Custom Separator here semi-colon\n");
readDataFromCustomSeparator(CSV_FILE_CUSTOM_SEPARATOR);
System.out.println("_______________________________________________");
}
public static void readDataLineByLine(String file)
{
try {
// Create an object of filereader class
// with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvReader object passing
// filereader as parameter
CSVReader csvReader = new CSVReader(filereader);
String[] nextRecord;
// we are going to read data line by line
while ((nextRecord = csvReader.readNext()) != null) {
for (String cell : nextRecord) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void readAllDataAtOnce(String file)
{
try {
// Create an object of filereader class
// with CSV file as a parameter.
FileReader filereader = new FileReader(file);
// create csvReader object
// and skip first Line
CSVReader csvReader = new CSVReaderBuilder(filereader)
.withSkipLines(1)
.build();
List allData = csvReader.readAll();
// print Data
for (String[] row : allData) {
for (String cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void readDataFromCustomSeparator(String file)
{
try {
// Create object of filereader
// class with csv file as parameter.
FileReader filereader = new FileReader(file);
// create csvParser object with
// custom separator semi-colon
CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
// create csvReader object with
// parameter filereader and parser
CSVReader csvReader = new CSVReaderBuilder(filereader)
.withCSVParser(parser)
.build();
// Read all data at once
List allData = csvReader.readAll();
// print Data
for (String[] row : allData) {
for (String cell : row) {
System.out.print(cell + "\t");
}
System.out.println();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
_______________________________________________
Read Data Line by Line With Header
name rollno department result cgpa
amar 42 cse pass 8.6
rohini 21 ece fail 3.2
aman 23 cse pass 8.9
rahul 45 ee fail 4.6
pratik 65 cse pass 7.2
raunak 23 me pass 9.1
suvam 68 me pass 8.2
_______________________________________________
Read All Data at Once and Hide the Header also
amar 42 cse pass 8.6
rohini 21 ece fail 3.2
aman 23 cse pass 8.9
rahul 45 ee fail 4.6
pratik 65 cse pass 7.2
raunak 23 me pass 9.1
suvam 68 me pass 8.2
_______________________________________________
Custom Separator here semi-colon
name rollno department result cgpa
amar 42 cse pass 8.6
rohini 21 ece fail 3.2
aman 23 cse pass 8.9
rahul 45 ee fail 4.6
pratik 65 cse pass 7.2
raunak 23 me pass 9.1
suvam 68 me pass 8.2
_______________________________________________
在以后的文章中,我们将包含更多使用 OpenCSV 对 CSV 文件进行的操作。
参考资料: CSVReader 类文档、OpenCSV 文档