2019年06月19日(星期三)  农历:己亥年五月十七

作者:三年。分类: JAVA

一.使用readNext()方法一行一行读CSV文件,返回字符串数组

package com.jiangge.csv.opencsvtest;

import java.io.FileReader;

import java.io.IOException;

import au.com.bytecode.opencsv.CSVReader;

//COUNTRY,CAPITAL,POPULATION

//India,New Delhi, 1.21B

//People's republic of China,Beijing, 1.34B

//United States,Washington D.C., 0.31B

/**

* use readNext() method of CSVReader class to read CSV file line by line.

* It returns a String array for each value in row.

*/

public class ReadLineByLine {

public static void main(String[] args) throws IOException {

String csvFilename = "C:\\sample.csv";

CSVReader csvReader = new CSVReader(new FileReader(csvFilename));

String[] row = null;

while((row = csvReader.readNext()) != null) {

System.out.println(row[0] + " # " + row[1] + " # " + row[2]);

}

//...

csvReader.close();

}

}

输出结果:

COUNTRY # CAPITAL # POPULATION

India # New Delhi # 1.21B

People's republic of China # Beijing # 1.34B

United States # Washington D.C. # 0.31B

二 使用 readAll()方法一次读取整个CSV文件,返回 List

package com.jiangge.csv.opencsvtest;

import java.io.FileReader;

import java.io.IOException;

import java.util.List;

import au.com.bytecode.opencsv.CSVReader;

//COUNTRY,CAPITAL,POPULATION

//India,New Delhi, 1.21B

//People's republic of China,Beijing, 1.34B

//United States,Washington D.C., 0.31B

/**

* read full CSV file once. The readAll() method of CSVReader class comes handy for this.

* The readAll() method returns a List of String[] for given CSV file.

* @author jiangge

*/

public class ReadAllTest {

public static void main(String[] args) throws IOException {

String[] row = null;

String csvFilename = "C:\\sample.csv";

CSVReader csvReader = new CSVReader(new FileReader(csvFilename));

List content = csvReader.readAll();

for (Object object : content) {

row = (String[]) object;

System.out.println(row[0] + " # " + row[1]+ " # " + row[2]);

}

//...

csvReader.close();

}

}

输出结果:

COUNTRY # CAPITAL # POPULATION

India # New Delhi # 1.21B

People's republic of China # Beijing # 1.34B

United States # Washington D.C. # 0.31B

三. 使用其他的分隔符号, 和跳过某些行--具体解释看这里:点击打开链接

CSVReader reader = new CSVReader(new FileReader(file), ';')

CSVReader reader = new CSVReader(new FileReader(file), '#')

跳过前5行,从第6行开始:

CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', 5);

四.Mapping CSV with Java beans

1.java bean

package com.jiangge.csv.opencsvtest;

public class Country {

private String countryName;

private String capital;

private String population;

public String getPopulation() {

return population;

}

public void setPopulation(String population) {

this.population = population;

}

public String getCountryName() {

return countryName;

}

public void setCountryName(String countryName) {

this.countryName = countryName;

}

public String getCapital() {

return capital;

}

public void setCapital(String capital) {

this.capital = capital;

}

}

2.关联JavaBean,Now we can map this bean with Opencsv and read the CSV file

package com.jiangge.csv.opencsvtest;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.InputStreamReader;

import java.util.List;

import au.com.bytecode.opencsv.CSVReader;

import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;

import au.com.bytecode.opencsv.bean.CsvToBean;

//COUNTRY,CAPITAL,POPULATION

//India,New Delhi, 1.21B

//People's republic of China,Beijing, 1.34B

//United States,Washington D.C., 0.31B

/**

* map this bean(Country.java) with Opencsv and read the CSV file. Check out below example:

* @author jiangge

*/

public class JavaBeanMapWithCSV {

public static void main(String[] args) throws FileNotFoundException {

ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();

strategy.setType(Country.class); // 关联JavaBean

String[] columns = new String[] {"countryName", "capital"}; // the fields to bind do in your JavaBean,JavaBean里的类变量属性

strategy.setColumnMapping(columns);

CsvToBean csv = new CsvToBean();

String csvFilename = "C:\\sample.csv";

CSVReader csvReader = new CSVReader(new FileReader(csvFilename));

// DataInputStream in = new DataInputStream(new FileInputStream(new File(csvFilename)));

// csvReader = new CSVReader(new InputStreamReader(in,"utf-8"),',', '\'', 1); //这两行注释掉的内容可以解决中文乱码问题

List list = csv.parse(strategy, csvReader);

for (Object object : list) {

Country country = (Country) object;

System.out.println(country.getCapital());

}

}

}

输出结果:

CAPITAL

New Delhi

Beijing

Washington D.C.

OpenCSV library中文乱码解决方法:

DataInputStream in = new DataInputStream(new FileInputStream(new File(csvFilename)));

csvReader = new CSVReader(new InputStreamReader(in,"utf-8"),',', '\'', 1); //这两行注释掉的内容可以解决中文乱码问题

温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,谢谢合作!

已有 0/1350 人参与

发表评论:



手Q扫描加入Java初学者群