0

Possible Duplicates:
Fast CSV parsing
How to properly parse CSV file to 2d Array?

I'm very new to java file handling. Please, can any one tell me what is "CSV file format," and how to parse this type of file?

I want to take an input of employee data from a CSV file and save it in a hash map.

7
  • Au contraire: stackoverflow.com/questions/6857248/fast-csv-parsing Commented Aug 30, 2011 at 16:48
  • stackoverflow.com/questions/2982828/java-csv-file-read-write also has some info Commented Aug 30, 2011 at 16:49
  • Very close to stackoverflow.com/q/3618521/183203 Commented Aug 30, 2011 at 16:49
  • 1
    @jleedev Part of amod0017's question was "what is csv" and in that sense it does not appear to be an exact duplicate. Commented Aug 30, 2011 at 16:49
  • 1
    @amod0017 Parsing CSV files is something that many people encounter and think is a very simple task. They then try it and realize they missed an edge case, and another edge case, and another edge case. They then try a library, but they realize the library they chose doesn't work well for some specific use-case. The whole deal is far more complicated than it would appear at first. Commented Aug 30, 2011 at 17:15

3 Answers 3

7

CSV stands for Comma Separated Values.

Here data is stored as so:

ID,Name,Age
20,"abcd xyz",33
30,asdf,28

OpenCSV is one good library for parsing CSV files.

There are other cousins of the CSV such as TSV (Tab Separated Values) and PSV (Pipe Separated Values). The below link should give you a head start:

http://en.wikipedia.org/wiki/Delimiter-separated_values

Sign up to request clarification or add additional context in comments.

2 Comments

hi i need to perfrom the parsing via my program... any suggestion???
Read the code snippets given at opencsv.sourceforge.net. They're pretty self-explanatory.
0

I also like the CSVReader library. The site includes code samples.

Comments

0

CSV - Comma Separated Value. The datas are separated using comma delimiter. Eg. 1,"johnson","software Engineer"

Code:

Public static void main (String args[])
{
try{

String fileName= "D:\\sample.csv";

        File file = new File(fileName);

        BufferedReader bufRdr = new BufferedReader(new FileReader(file));
        String line = null;


        ArrayList arraylist=new ArrayList();

        // Read each line of text in the file
        while((line = bufRdr.readLine()) != null) 
    {
    String[] data=line.split(",");      
    HashMap hm=new HashMap();
    hm("employeeID",data[0]);
    hm("employeeName",data[1]);
    hm("employeeDesignation",data[2]);
    arraylist.add(hm);
    }

}catch(Exception e){}

}

2 Comments

This doesn't work if the data contains commas. So "johnson" works as a name but "Johnson, Mike" does not.
It also includes the quotes in the values being stored in the HashMap. My recommendation is to NOT parse CSV using split or Scanner or string bashing. Use (or write) a proper CSV parser.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.