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.

Community
  • 1
  • 1
amod
  • 4,190
  • 10
  • 49
  • 75
  • Au contraire: http://stackoverflow.com/questions/6857248/fast-csv-parsing – Josh Lee Aug 30 '11 at 16:48
  • http://stackoverflow.com/questions/2982828/java-csv-file-read-write also has some info – Mat Aug 30 '11 at 16:49
  • Very close to http://stackoverflow.com/q/3618521/183203 – antlersoft Aug 30 '11 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. – Michael McGowan Aug 30 '11 at 16:49
  • It would have been faster to google for the answer. – Peter Lawrey Aug 30 '11 at 17:02
  • @ all who voted for close... please note that i am asking whats csv however i got a near most answer to it... but i need a program/code to parse in spite using some library. – amod Aug 30 '11 at 17:09
  • 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. – Michael McGowan Aug 30 '11 at 17:15

3 Answers3

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

adarshr
  • 61,315
  • 23
  • 138
  • 167
0

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

andronikus
  • 4,125
  • 2
  • 29
  • 46
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
    This doesn't work if the data contains commas. So "johnson" works as a name but "Johnson, Mike" does not. – Michael McGowan Aug 30 '11 at 18:29
  • 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. – Stephen C Dec 21 '20 at 23:05