Reading CSV file in resources folder android

you should put csv file in assets folder ..

InputStreamReader is = new InputStreamReader(getAssets()
                        .open("filename.csv"));

BufferedReader reader = new BufferedReader(is);
reader.readLine();
String line;
while ((line = reader.readLine()) != null) {
                        
}

As an alternative, take a look at uniVocityParsers. It provides a vast number of ways to parse delimited files. The example bellow loads a Csv File (see in the picture below) from a res/raw folder into a InputStream object, and read it in a colunar manner (a map where key=Column & value=ColumnValues).

calendario_bolsa.csv

//Gets your csv file from res/raw dir and load into a InputStream.
InputStream csvInputStream = getResources().openRawResource(R.raw.calendario_bolsa);

//Instantiate a new ColumnProcessor
ColumnProcessor columnProcessor = new ColumnProcessor();

//Define a class that hold the file configuration
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.getFormat().setLineSeparator("\n");
parserSettings.setHeaderExtractionEnabled(true);
parserSettings.setProcessor(columnProcessor);

//Creates a new CsvParser, passing the settings into its construtor:
CsvParser csvParser = new CsvParser(parserSettings);

//Calls parse method, instantiating an InputStreamReader, passing to its constructor the InputStream object
csvParser.parse(new InputStreamReader(csvInputStream));

//Gets the csv data as a Map of Column / column values.
Map<String, List<String>> columnarCsv = columnProcessor.getColumnValuesAsMapOfNames();

To add univocityParsers into your Android Project:

compile group: 'com.univocity', name: 'univocity-parsers', version: '2.3.0'

Some advices;

  • Create an object for holding one row data into the csv. ( Ex: YourSimpleObject . It provides you to manage the data easily.)
  • Read file row by row and assign to object. Add the object to list. (Ex: ArrayList<YourSimpleObject >)

Code:

private void readAndInsert() throws UnsupportedEncodingException {


ArrayList<YourSimpleObject > objList= new ArrayList<YourSimpleObject >();
AssetManager assetManager = getAssets();
InputStream is = null;

            try {
                is = assetManager.open("questions/question_bank.csv");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            BufferedReader reader = null;
            reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));

            String line = "";
            StringTokenizer st = null;
            try {

                while ((line = reader.readLine()) != null) {
                    st = new StringTokenizer(line, ",");
                    YourSimpleObject obj= new YourSimpleObject ();
                                    //your attributes
                    obj.setX(st.nextToken());
                    obj.setY(st.nextToken());
                    obj.setZ(st.nextToken());
                    obj.setW(st.nextToken());

                    objList.add(sQuestion);

                }
            } catch (IOException e) {

                e.printStackTrace();
            }



}

Using opencsv:

InputStream is = context.getAssets().open(path);
InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8"));
List<String[]> csv = new CSVReader(reader).readAll();