欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java读写CSV之SuperCSV

程序员文章站 2022-03-02 12:00:42
...
Super CSV是一个速度奇快、免费跨平台的 CSV 格式数据的读写库,可以方便的处理对象、Map、列表的读写操作,以及自动化的类型转换和数据检查功能。

http://supercsv.sourceforge.net/

版本:SuperCSV-1.52.jar


1、读CSV(CsvBeanReader/CsvListReader/CsvMapReader)
InputStreamReader freader = new InputStreamReader(new FileInputStream(
		new File("csv/test1.csv")), "GB2312");

ICsvBeanReader reader = new CsvBeanReader(freader,
		CsvPreference.EXCEL_PREFERENCE);

//获取头部信息
String[] headers = reader.getCSVHeader(true);

//获取数据部分
UserBean bean = null;
while ((bean = reader.read(UserBean.class, headers, UserBean.readProcessors)) != null) {
	System.out.print(bean.getName() + "\t");
	System.out.print(bean.getAge() + "\t");
	System.out.print(bean.getBirthday() + "\t");
	System.out.println(bean.getAddress());
}


2、写CSV(CsvBeanWriter/CsvListWriter/CsvMapWriter)
OutputStreamWriter fwriter = new OutputStreamWriter(
		new FileOutputStream(new File("csv/test2.csv")), "GB2312");

SimpleDateFormat format = new SimpleDateFormat("yyyy/M/d");

ICsvBeanWriter writer = new CsvBeanWriter(fwriter,
		CsvPreference.EXCEL_PREFERENCE);

//做3条数据
UserBean bean1 = new UserBean();
bean1.setName("赵,四");		//特殊字符:逗号
bean1.setAge(26);
bean1.setBirthday(format.parse("1984/10/30"));
bean1.setAddress("辽宁");

UserBean bean2 = new UserBean();
bean2.setName("刘\n能");		//特殊字符:换行
bean2.setAge(24);
bean2.setBirthday(format.parse("1986/5/13"));
bean2.setAddress("吉林");

UserBean bean3 = new UserBean();
bean3.setName("谢\"广坤");	//特殊字符:双引号
bean3.setAge(22);
bean3.setBirthday(format.parse("1988/10/8"));
bean3.setAddress("黑龙江");

//输出头部
String headers[] = { "name", "age", "birthday", "address" };		
writer.writeHeader(headers);

//按顺序输出数据
writer.write(bean1, headers, UserBean.writeProcessors);
writer.write(bean2, headers, UserBean.writeProcessors);
writer.write(bean3, headers, UserBean.writeProcessors);

writer.close();


3、自定义CellProcessor
InputStreamReader freader = new InputStreamReader(new FileInputStream(
		new File("csv/test5.csv")), "GB2312");

ICsvBeanReader reader = new CsvBeanReader(freader,
		CsvPreference.EXCEL_PREFERENCE);

//获取头部信息
String[] headers = reader.getCSVHeader(true);

CellProcessor[] readProcessors = new CellProcessor[] {
		new StrMinMax(2, 10),
		new ParseInt(),
		new ParseDate("yyyy/M/d"),
		new CellProcessorAdaptor() {
			@Override
			public Object execute(final Object value, final CSVContext context) {
				String v = (String) value;
				if (v.length() > 5) {
					v = v.substring(0,5) + "......";
				}
		        return next.execute(v, context);
			}
		}};

//获取数据部分
UserBean bean = null;
while ((bean = reader.read(UserBean.class, headers, readProcessors)) != null) {
	System.out.print(bean.getName() + "\t");
	System.out.print(bean.getAge() + "\t");
	System.out.print(bean.getBirthday() + "\t");
	System.out.println(bean.getAddress());
}
System.out.println();

reader.close();


其他的CSV解析器还有:
Commons CSV:http://commons.apache.org/proper/commons-csv/
OpenCSV:http://opencsv.sourceforge.net/
OrangeSignal CSV:http://orangesignal.github.io/orangesignal-csv/

CSV Library Features Matrix
http://orangesignal.github.io/orangesignal-csv/csv_library_feature_matrix.html
https://github.com/uniVocity/csv-parsers-comparison
相关标签: java csv SuperCSV