Reputation: 219
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...
Upvotes: 14
Views: 32355
Reputation: 26984
You are iterating twice in each loop
String line;
while ((line=r.readLine()) != null) {
myDict.add(line);
}
Upvotes: 22
Reputation: 6087
Using Apache IOUtils:
List<String> lines = IOUtils.readLines(inputStream, "UTF-8");
Upvotes: 19