Reading InputStream for Arraylist

I need to read a dict.txt file that contains one line for a line and add them to arraylist.

I tried this:

public ArrayList<String> myDict = new ArrayList<String>(); InputStream is = (getResources().openRawResource(R.raw.dict)); BufferedReader r = new BufferedReader(new InputStreamReader(is)); try { while (r.readLine() != null) { myDict.add(r.readLine()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

but something is wrong ...

+6
source share
2 answers

You repeat twice in each cycle

 String line; while ((line=r.readLine()) != null) { myDict.add(line); } 
+12
source

Using Apache IOUtils:

 List<String> lines = IOUtils.readLines(inputStream, "UTF-8"); 
+8
source

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


All Articles