Alex Rivera | Logout

Java reading a file into an ArrayList?

Asked 2011-03-17T18:42:23.380
83

How do you read the contents of a file into an ArrayList<String> in Java?

Here are the file contents:

cat
house
dog
.
.
.
Edit
Report

1 Answer

6
List<String> words = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader("words.txt"));
String line;
while ((line = reader.readLine()) != null) {
    words.add(line);
}
reader.close();
answered 2011-03-17T18:45:14.530

Your Answer