---
title: "Apache commons-csv使用"
author: "静默时间-花开灿然"
date: "2015-07-05T16:51:20Z"
tags: []
categories: ["研发管理"]
slug: "apache-commons-csv"
draft: false
lastmod: "2015-07-05T16:51:20Z"
id: 2224404
url: "https://zhen217.iteye.com/blog/2224404"
---

Apache commons下有个commons-csv的jar包，用来读取csv格式的纯文本文件。

csv文件内容如下：

name,age  
wangqiang,21  
lirui,19  
qijin,33  
zouying,28

 读取本地文件：

```
/**
	 * @param args
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException{
		CSVParser parser = new CSVParser(new FileReader("test.csv"), CSVFormat.DEFAULT.withHeader());
		for(CSVRecord record:parser){
			System.out.printf("%s\t%s\n",record.get("name"), record.get("age"));  

		}
		parser.close();
	}
	/**
	 * 结果：
	 	wangqiang	21
		lirui	19
		qijin	33
		zouying	28
	 */
```