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(); } }
source share