简体   繁体   中英

How To print multiple Columns from Csv using java

Hi I am trying to fetch specific data from .csv file and the code i used is

import java.io.BufferedReader;
import java.io.FileReader;

public class InsertValuesIntoTestDb {


    public static void main(String[] args) throws Exception {


        String splitBy = ",";

        BufferedReader br = new BufferedReader(newFileReader("test.csv"));


        String line = br.readLine();

        while ((line = br.readLine()) !=null) {

             String[] b = line.split(splitBy);

             System.out.println(b[0]);
        }

        br.close();

  }

}

and the .csv looks like

a,f,w,b,numinst,af,ub

1RW,800,64,22,1,48:2,true

1RW,800,16,39,1,48:2,true

1RW,800,640,330,1,48:2,true

1RW,800,40,124,1,48:2,true

1RW,800,32,104,1,48:2,true

1RW,800,8,104,1,48:2,true

1R1W,800,65536,39,1,96:96,true

1R1W,800,2048,39,1,96:96,true

1R1W,800,8192,39,1,48:48,true

with the above code i can print only column 'a' and my o/p looks like

a


1RW

1Rw

1Rw 

like this but how can i print column f,w,b ??

my output should look like

f   w   b 

800 64 22

800 64 22

800 64 22

Thanks

Simply change System.out.println(b[0]); to System.out.println(b[1]+" "+b[2]+" "+b[3]);

To show all values in one line you could change the while loop like this:

    while ((line = br.readLine()) !=null) {
         String[] b = line.split(splitBy);
         if (b.length > 3) {
           System.out.println(b[1] + ' ' + b[2] + ' ' + b[3] + ' ');
         } else {
           System.err.println("Not enough values in line " + line);
         }
    }

This will first check, if there are at least 4 columns and then print the 2nd, 3rd and 4th value.

Hope this helps.

Using String.split to parse CSV is not only unreliabe, it is also slow. You'll have problems parsing values that may contain the comma character, line separators, etc. Additionally, you need to take care of rows with varying number of columns to avoid ArrayIndexOutOfBoundsException .

Use a CSV parser for that. uniVocity-parsers makes it easy for you to get the columns of your CSV:

CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setHeaderExtractionEnabled(true);
parserSettings.selectFields("f", "w", "b");

CsvParser parser = new CsvParser(parserSettings);
List<String[]> parsedRows = parser.parseAll(newFileReader("test.csv"));

If you want to parse your CSV and get a list of values for each column, you can use a RowProcessor and do this:

CsvParserSettings parserSettings = new CsvParserSettings(); //many options here, check the documentation.
parserSettings.setHeaderExtractionEnabled(true); // gets the headers from the first row

// To get the values of all columns, use a column processor
ColumnProcessor rowProcessor = new ColumnProcessor();
parserSettings.setRowProcessor(rowProcessor);

CsvParser parser = new CsvParser(parserSettings);

//This will parse all rows and submit them to the column processor
parser.parse(newFileReader("test.csv"));

//Finally, we can get the column values:
Map<String, List<String>> columnValues = rowProcessor.getColumnValuesAsMapOfNames();

Empty rows are skipped by default (if you want them then configure the parser to not skip empty rows). The lists in the columnValues map will have the exact same size, and if a row of your input has less columns than the others, the corresponding value will be null .

Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM