开发者

What is csv file and how to parse it using java code? [duplicate]

This question already has answers here: Closed 11 years ago.

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.


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


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


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){}

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜