package package3;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class TxtToArrayListDemo {
public static void main(String[] args) throws IOException {
//创建字符缓冲输入流对象
BufferedReader br =new BufferedReader(new FileReader("day04\\src\\package3\\array.txt"));
//创建ArrayList集合
ArrayList<String> list =new ArrayList<>();
//调用字符缓冲输入流对象的方法读数据
String line ;
while ((line=br.readLine())!=null){
//把读取到的数据存储到集合中
list.add(line);
}
//释放资源
br.close();
//验证
for(String s:list){
System.out.println(s);
}
}
}
标签:遍历,java,io,ArrayList,文本文件,集合,import,String
From: https://www.cnblogs.com/knlf/p/16813969.html