Java reading a file into an ArrayList?

后端 未结 13 1234
醉话见心
醉话见心 2020-11-22 12:05

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

Here are the file contents:

cat
ho         


        
13条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 12:48

    List words = new ArrayList();
    BufferedReader reader = new BufferedReader(new FileReader("words.txt"));
    String line;
    while ((line = reader.readLine()) != null) {
        words.add(line);
    }
    reader.close();
    

提交回复
热议问题