如何在Java中讀取.csv檔案的資料?
名為OpenCSV的庫提供了用於讀取和寫入.CSV檔案的API。這裡解釋瞭如何使用Java程式讀取.csv檔案的內容。
Maven依賴
<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檔案的內容。
使用readNext()方法
CSVReader類的readNext()方法讀取.csv檔案的下一行,並將其以字串陣列的形式返回。
示例
下面的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
使用readAll()方法
此方法一次性將.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]
使用迭代器
除了上述兩種方法外,還可以獲取CSVReader物件的迭代器,並使用迭代器的hasNext()和next()方法讀取.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]
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP