将集合ArrayList中的元素写入文本文档; ArrayList<String> list = new ArrayList<>(); list.add("张飞"); list.add("关羽"); list.add("马超"); list.add("黄忠"); list.add("赵云"); //遍历集合,写出集合数据 BufferedWriter bfw = new BufferedWriter(new FileWriter("name88.txt")); for (String s : list) { bfw.write(s);//通过高效流向txt文件中写入集合中的字符串元素; bfw.newLine();//换行 bfw.flush();//刷新 } //释放资源 bfw.close(); 将文本文档中的内容传入集合; //创建字符输入流 BufferedReader bfr = new BufferedReader(new FileReader("name88.txt")); ArrayList<String> list = new ArrayList<>(); while (true){ String s = bfr.readLine();//读取指定文本文件每一行的字符,返回一个字符串 if(s==null){//如果读完就跳出循环 break; } list.add(s);//向集合中添加读取到的字符串 } bfr.close(); System.out.println(list);