Wikipedia: Click to open the link to separate Values (Comma-Separated Values, CSV, also knownDelimiter ValueBecause the delimiter can also be a comma (,), its File Stores Table data (numbers and text) in plain text format ). Plain text means that the file is a character sequence and does not contain data that must be interpreted as a binary number. A csv file consists of any number of records separated by a line break. Each record is composed of fields. The separators between fields are other characters or strings, the most common is comma or tab. Generally, all records have exactly the same field sequence.
Example-basic rules and examples-source Wikipedia (note comma, quotation marks, line feed) Click to open the link
| Year |
Brand |
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 finished!
Air, moon roof, loaded |
4799.00 |
The preceding table can be expressed as follows in CSV format:
Year,Make,Model,Description,Price1997,Ford,E350,ac, abs, moon,3000.001999,Chevy,Venture Extended Edition,,4900.001999,Chevy,Venture Extended Edition, Very Large,,5000.001996,Jeep,Grand Cherokee,MUST SELL!air, moon roof, loaded,4799.00
The following code example:
1. Read CSV files:
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,Thailandpublic 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 separatorString[] 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); } }
Console output result:
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
The core code is actually very simple:
Try {br = new BufferedReader (new FileReader (csvFile); while (line = br. readLine ())! = Null) {String [] country = line. split (cvsSplitBy); // use comma as separator using comma as the separator System. out. println (Country [code = + country [4] +, name = + country [5] +]);}
Ii. Read the CSV file and use Map to remove duplicates
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
maps = new HashMap
();br = new BufferedReader(new FileReader(csvFile));while ((line = br.readLine()) != null) {String[] country = line.split(cvsSplitBy); //// use comma as separatormaps.put(country[4], country[5]);} //loop mapfor (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); } }
Console output result:
Country [code= TH , name=Thailand]Country [code= CN , name=China]Country [code= JP , name=Japan]Country [code= AU , name=Australia]Done
Core code: the key of the Map cannot be repeated. It is very simple :-)
try {Map
maps = new HashMap
();br = new BufferedReader(new FileReader(csvFile));while ((line = br.readLine()) != null) {String[] country = line.split(cvsSplitBy); //// use comma as separatormaps.put(country[4], country[5]);} //loop mapfor (Map.Entry
entry : maps.entrySet()) {System.out.println(Country [code= + entry.getKey() + , name=+ entry.getValue() + ]);}