1、mvn依賴
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>
2、代碼
public List<String> readCSV(String filename) {
CsvReader reader = null;
FileInputStream fin = null;
List<String> list = new ArrayList<String>();
try {
fin = new FileInputStream(filename);
Charset charset = Charset.forName("gbk");
reader = new CsvReader(fin, charset);
while (reader.readRecord()) {
int icount = reader.getColumnCount();//獲取一共多少列數據
if (icount > 0) {
if (!list.contains(reader.get(0).toLowerCase()))//獲取第一列數據
list.add(reader.get(0).toLowerCase());
}
// String str = reader.getRawRecord();//讀取某行
// System.out.println(str);
}
if (list.size() > 0) {
if ("搜索詞".equals(list.get(0))) {
list.remove(0);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fin != null)
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
reader.close();
}
return list;
}