黑马程序员技术交流社区
标题:
io 复制文本存入集合
[打印本页]
作者:
fmi110
时间:
2015-9-8 16:40
标题:
io 复制文本存入集合
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();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2