Java CSV读--Read And Parse CSV File In Java

3 篇文章 0 订阅


维基百科: 点击打开链接 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须象二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。通常,所有记录都有完全相同的字段序列。


举例-基本规则及举例--来源维基百科(注意逗号,引号,换行)点击打开链接

年份 品牌 型号 描述 价格
1997FordE350ac, abs, moon3000.00
1999ChevyVenture "Extended Edition" 4900.00
1999ChevyVenture "Extended Edition, Very Large" 5000.00
1996JeepGrand CherokeeMUST SELL!
air, moon roof, loaded
4799.00

以上数据表可以以CSV格式表示如下:

Year,Make,Model,Description,Price
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00


以下为代码事例:

一.读CSV文件:

package com.jiangge.csv;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

//"1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
//"1.0.1.0","1.0.3.255","16777472","16778239","CN","China"
//"1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
//"1.0.8.0","1.0.15.255","16779264","16781311","CN","China"
//"1.0.16.0","1.0.31.255","16781312","16785407","JP","Japan"
//"1.0.32.0","1.0.63.255","16785408","16793599","CN","China"
//"1.0.64.0","1.0.127.255","16793600","16809983","JP","Japan"
//"1.0.128.0","1.0.255.255","16809984","16842751","TH","Thailand"


public class ReadCVSMkYong {
	  public static void main(String[] args) {
		ReadCVSMkYong obj = new ReadCVSMkYong();
		obj.run();
	  }
	 
	  public void run() {
		String csvFile = "c:/GeoIPCountryWhois.csv";
		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ",";
	 
		try {
			br = new BufferedReader(new FileReader(csvFile));
			while ((line = br.readLine()) != null) {
			    // use comma as separator
				String[] country = line.split(cvsSplitBy);
				System.out.println("Country [code= " + country[4]  + " , name=" + country[5] + "]");
			}
	 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	 
		System.out.println("Done");
	  }
	 
	}
	


控制台输出结果:

Country [code= "AU" , name="Australia"]
Country [code= "CN" , name="China"]
Country [code= "AU" , name="Australia"]
Country [code= "CN" , name="China"]
Country [code= "JP" , name="Japan"]
Country [code= "CN" , name="China"]
Country [code= "JP" , name="Japan"]
Country [code= "TH" , name="Thailand"]
Done

核心代码其实很简单:

	try {
			br = new BufferedReader(new FileReader(csvFile));
			while ((line = br.readLine()) != null) {
				String[] country = line.split(cvsSplitBy);  // use comma as separator 使用逗号做为分隔符
				System.out.println("Country [code= " + country[4]  + " , name=" + country[5] + "]");
			}
	 
		}



二.读CSV文件,使用 Map 去重

package com.jiangge.csv;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 

/**
 * This example show you how to use Map to remove the duplicated country code and country name.
 * @author jiangge
 */
public class ReadCVSMkYong2 {
  public static void main(String[] args) {
	  ReadCVSMkYong2 obj = new ReadCVSMkYong2();
	  obj.run();
  }
 
  public void run() {
	String csvFile = "c:/GeoIPCountryWhois.csv";
	BufferedReader br = null;
	String line = "";
	String cvsSplitBy = ",";
 
	try {
		Map<String, String> maps = new HashMap<String, String>();
		br = new BufferedReader(new FileReader(csvFile));
		while ((line = br.readLine()) != null) {
			String[] country = line.split(cvsSplitBy);  use comma as separator
			maps.put(country[4], country[5]);
		}
 
		//loop map
		for (Map.Entry<String, String> entry : maps.entrySet()) {
			System.out.println("Country [code= " + entry.getKey() + " , name="+ entry.getValue() + "]");
		}
 
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		if (br != null) {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
 
	System.out.println("Done");
  }
 
}

控制台输出结果:

Country [code= "TH" , name="Thailand"]
Country [code= "CN" , name="China"]
Country [code= "JP" , name="Japan"]
Country [code= "AU" , name="Australia"]
Done

核心代码: Map的 key 不能重复,很简单的吧 :-)

	try {
		Map<String, String> maps = new HashMap<String, String>();
		br = new BufferedReader(new FileReader(csvFile));
		while ((line = br.readLine()) != null) {
			String[] country = line.split(cvsSplitBy);  use comma as separator
			maps.put(country[4], country[5]);
		}
 
		//loop map
		for (Map.Entry<String, String> entry : maps.entrySet()) {
			System.out.println("Country [code= " + entry.getKey() + " , name="+ entry.getValue() + "]");
		}


参考资料:

1.维基百科 http://zh.wikipedia.org/wiki/%E9%80%97%E5%8F%B7%E5%88%86%E9%9A%94%E5%80%BC

2.How To Read And Parse CSV File In Java http://www.mkyong.com/java/how-to-read-and-parse-csv-file-in-java/





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值