14

I have to read a dict.txt file which contains one string for line and add these to an 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 wrong...

1
  • you are reading two times one in while and second time while adding to list. Commented Dec 16, 2011 at 9:14

2 Answers 2

22

You are iterating twice in each loop

String line;
while ((line=r.readLine()) != null) {
    myDict.add(line);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Should the "line" variable in your response be of type ArrayList?
19

Using Apache IOUtils:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.