bj.run();
}
public void run() {
String csvFile = c:/GeoIPCountryWhois.csv;
BufferedReader br = null;
String line = ;
String cvsSplitBy = ,;
try {
Map
maps = new HashMap
(); 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
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
maps = new HashMap
(); 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
entry : maps.entrySet()) { System.out.println(Country [code= + entry.getKey() + , name=+ entry.getValue() + ]); }