名为OpenCSV的库提供API来从.CSV文件读取数据或将数据写入.CSV文件。此处说明了如何使用Java程序读取.csv文件的内容。
<dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>4.4</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency>
com.opencsv软件包的CSVReader类表示一个简单的CSV阅读器。在实例化此类时,您需要将表示要读取的文件的Reader对象作为参数传递给其构造函数。它提供了名为readAll()和readNext()的方法来读取.csv文件的内容
CSVReader类的readNext()方法读取.csv文件的下一行,并以String数组的形式返回。
以下Java程序演示了如何使用readNext()方法读取.csv文件的内容。
import java.io.FileReader;
import com.opencsv.CSVReader;
public class ReadFromCSV {
public static void main(String args[]) throws Exception {
//Instantiating the CSVReader class
CSVReader reader = new CSVReader(new FileReader("D://sample.csv"));
//Reading the contents of the csv file
StringBuffer buffer = new StringBuffer();
String line[];
while ((line = reader.readNext()) != null) {
for(int i = 0; i<line.length; i++) {
System.out.print(line[i]+" ");
}
System.out.println(" ");
}
}
}输出结果
id name salary start_date dept 1 Rick 623.3 2012-01-01 IT 2 Dan 515.2 2013-09-23 Operations 3 Michelle 611 2014-11-15 IT 4 Ryan 729 2014-05-11 HR 5 Gary 843.25 2015-03-27 Finance 6 Nina 578 2013-05-21 IT 7 Simon 632.8 2013-07-30 Operations 8 Guru 722.5 2014-06-17 Finance
此方法将.csv文件的内容立即读取到String数组类型的List对象中。
以下Java程序演示了如何使用readAll()方法读取.csv文件的内容。
import java.io.FileReader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import com.opencsv.CSVReader;
public class ReadFromCSV {
public static void main(String args[]) throws Exception {
//Instantiating the CSVReader class
CSVReader reader = new CSVReader(new FileReader("D://sample.csv"));
//Reading the contents of the csv file
List list = reader.readAll();
//Getting the Iterator object
Iterator it = list.iterator();
while(it.hasNext()) {
String[] str = (String[]) it.next();
System.out.println(Arrays.toString(str));
}
}
}输出结果
[id, name, salary, start_date, dept] [1, Rick, 623.3, 2012-01-01, IT] [2, Dan, 515.2, 2013-09-23, Operations] [3, Michelle, 611, 2014-11-15, IT] [4, Ryan, 729, 2014-05-11, HR] [5, Gary, 843.25, 2015-03-27, Finance] [6, Nina, 578, 2013-05-21, IT] [7, Simon, 632.8, 2013-07-30, Operations] [8, Guru, 722.5, 2014-06-17, Finance]
除了以上两种方法,您还可以使用Iterator的hasNext()和next()方法获取CSVReader对象的迭代器并读取.csv文件的内容。
import java.io.FileReader;
import java.util.Arrays;
import java.util.Iterator;
import com.opencsv.CSVReader;
public class ReadFromCSV {
public static void main(String args[]) throws Exception {
//Instantiating the CSVReader class
CSVReader reader = new CSVReader(new FileReader("D://sample.csv"));
//Reading the contents of the csv file
StringBuffer buffer = new StringBuffer();
String line[];
//Getting the iterator object for this reader
Iterator it = reader.iterator();
while (it.hasNext()) {
line = (String[]) it.next();
System.out.println(Arrays.toString(line));
System.out.println(" ");
}
}
}输出结果
[id, name, salary, start_date, dept] [1, Rick, 623.3, 2012-01-01, IT] [2, Dan, 515.2, 2013-09-23, Operations] [3, Michelle, 611, 2014-11-15, IT] [4, Ryan, 729, 2014-05-11, HR] [5, Gary, 843.25, 2015-03-27, Finance] [6, Nina, 578, 2013-05-21, IT] [7, Simon, 632.8, 2013-07-30, Operations] [8, Guru, 722.5, 2014-06-17, Finance]