Read CSV file column by column

前端 未结 8 1684
萌比男神i
萌比男神i 2020-11-29 04:31

I want to read specific columns from a multi column csv file and print those columns in other csv file using Java. Any help please? Following is my code to print each token

8条回答
  •  無奈伤痛
    2020-11-29 05:20

    Reading a CSV file in very simple and common in Java. You actually don't require to load any extra third party library to do this for you. CSV (comma separated value) file is just a normal plain-text file, store data in column by column, and split it by a separator (e.g comma ",").

    In order to read specific columns from the CSV file, there are several ways. Simplest of all is as below:

    Code to read CSV without any 3rd party library

    BufferedReader br = new BufferedReader(new FileReader(csvFile));
    while ((line = br.readLine()) != null) {
        // use comma as separator
        String[] cols = line.split(cvsSplitBy);
        System.out.println("Coulmn 4= " + cols[4] + " , Column 5=" + cols[5]);
    }
    

    If you notice, nothing special is performed here. It is just reading a text file, and spitting it by a separator – ",".

    Consider an extract from legacy country CSV data at GeoLite Free Downloadable Databases

    "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","Thailand"
    

    Above code will output as below:

    Column 4= "AU" , Column 5="Australia"
    Column 4= "CN" , Column 5="China"
    Column 4= "AU" , Column 5="Australia"
    Column 4= "CN" , Column 5="China"
    Column 4= "JP" , Column 5="Japan"
    Column 4= "CN" , Column 5="China"
    Column 4= "JP" , Column 5="Japan"
    Column 4= "TH" , Column 5="Thailand"
    

    You can, in fact, put the columns in a Map and then get the values simply by using the key.

    Shishir

提交回复
热议问题