A comma-separated values (CSV) file is just a normal plain-text file, store data in column by column, and split it by a separator (e.g comma “,”). For example :
US,United States MY,Malaysia
In this tutorial, we show you how to read, parse and print out the values from a csv file.
1. CSV file
Get a country CVS file sample from http://dev.maxmind.com/geoip/geolite
GeoIPCountryWhois.cvs
"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" //... skip, too large
2. Example 1
Extracts the country code and country name from above CVS file, and print it out. No magic, just read a text file, and spits it by a separator – “,”.
ReadCVS.java
package com.mkyong.util; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class ReadCVS { public static void main(String[] args) { ReadCVS obj = new ReadCVS(); obj.run(); } public void run() { String csvFile = "/Users/mkyong/Downloads/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"); } }
Output
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"] //skip
3. Example 2
This example show you how to use
Map
to remove the duplicated country code and country name.
ReadCVS.java
package com.mkyong.util; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class ReadCVS { public static void main(String[] args) { ReadCVS obj = new ReadCVS(); obj.run(); } public void run() { String csvFile = "/Users/mkyong/Downloads/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) { // use comma as separator String[] country = line.split(cvsSplitBy); 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"); } }
Output
Country [code= "AU" , name="Australia"] Country [code= "CN" , name="China"] Country [code= "JP" , name="Japan"] Country [code= "TH" , name="Thailand"] //skip
No comments:
Post a Comment