a- package test;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- public class TxtToCollection {
- /**
- * 需求:从文本文件读取行内容到集合
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- //建立集合
- ArrayList<String> al = new ArrayList<String>();
- //建里高效读取文本流
- BufferedReader br = new BufferedReader(new FileReader("transformStreamDemo.txt"));
-
- String line = null;
- //按行读取
- while((line = br.readLine())!=null)
- al.add(line); //存入集合
- //遍历集合
- for(String s:al){
- System.out.println(s);
- }
- //释放资源
- br.close();
- }
-
- }
复制代码
|
|