How to read and parse CSV file in Java

How to read and parse CSV file in Java



http://dev.maxmind.com/geoip/geolite download .csv

package com.satish.util;

 import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
 import java.io.IOException;

 public class demo
 {
 public static void main(String[] args)
 {
  demo obj = new demo(); obj.run();
 }

public void run()
{
    String csvFile = "/Users/satish/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"]

Post a Comment

0 Comments

Recent in Recipes

3/Food/post-list