2

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

3 Answers 3

1

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

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

3 Comments

thank you but when i run it its shows some exception like this Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at ReadCVS.main(ReadCVS.java:21)
Do you have a Line without "," in you file?
So you have make an if (b.length>4) arround the print statement
0

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.

2 Comments

I have more than 4 columns but thing is i have some empty columns in between will it affect??
Try it out, but the only affect shoud be that empty lines will result in the System.err.println()s
0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.