Reading CSV file in android folder

I am developing an android application in netbeans. I am trying to read a CSV file using opencsv. When I put a file in the resource folder and try to read it from there, an error occurs when creating a message about an invalid resource directory. Where should I store the csv file so that it can be read every time the application starts?

+6
source share
5 answers

you must put the csv file in the resource folder.

InputStreamReader is = new InputStreamReader(getAssets() .open("filename.csv")); BufferedReader reader = new BufferedReader(is); reader.readLine(); String line; while ((line = reader.readLine()) != null) { } 
+7
source

Some tips;

  • Create an object to store single row data in csv. ( Ex: YourSimpleObject ). This makes it easy to manage data.)
  • Read the file line by line and assign to the object. Add an object to the list. (Example: 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(); } } 
+7
source

you can use this code

  try { InputStream csvStream = assetManager.open(CSV_PATH); InputStreamReader csvStreamReader = new InputStreamReader(csvStream); CSVReader csvReader = new CSVReader(csvStreamReader); String[] line; // throw away the header csvReader.readNext(); while ((line = csvReader.readNext()) != null) { questionList.add(line); } } catch (IOException e) { e.printStackTrace(); } 

you can download the csvreader file from http://sourceforge.net/projects/opencsv/files/latest/download

and import into your project

+1
source

Alternatively take a look at uniVocityParsers . It provides a huge number of ways to parse delimited files. The example below loads the Csv file (see the figure below) from the res / raw folder into the InputStream object and reads it in colunar mode (map, where key = Column and 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 to your Android project:

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

Using opencsv:

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

Source: https://habr.com/ru/post/958058/


All Articles