-1

How can I access data in a CSV file, like I want data from from a certain cell and store it in a variable, but without using SQL. Any references would be also great to read about it

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Weigel Gram
  • 185
  • 1
  • 2
  • 11

2 Answers2

1
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import au.com.bytecode.opencsv.CSVReader;

public class CsvFileReader {
    public static void main(String[] args) {

        try {
            System.out.println("\n**** readLineByLineExample ****");
            String csvFilename = "C:/Users/hussain.a/Desktop/temp/20130128.csv";
            CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
            String[] col = null;
            while ((col = csvReader.readNext()) != null) {
                System.out.println(col[0] + " # " + col[1]);
                //System.out.println(col[0]);
            }
            csvReader.close();
        } catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

you can attach the jar from here

0

You need to open the CSV file, run it through a CSV parser, and use the API on the parser to extract whatever information you need.

http://opencsv.sourceforge.net/

NickJ
  • 9,380
  • 9
  • 51
  • 74