黑马程序员技术交流社区

标题: io 复制文本存入集合 [打印本页]

作者: fmi110    时间: 2015-9-8 16:40
标题: io 复制文本存入集合
a
  1. package test;

  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.ArrayList;

  7. public class TxtToCollection {

  8.         /**
  9.          * 需求:从文本文件读取行内容到集合
  10.          * @throws IOException
  11.          */
  12.         public static void main(String[] args) throws IOException {
  13.                 // TODO Auto-generated method stub
  14.                 //建立集合
  15.                 ArrayList<String> al = new ArrayList<String>();
  16.                 //建里高效读取文本流
  17.                 BufferedReader br = new BufferedReader(new FileReader("transformStreamDemo.txt"));
  18.                
  19.                 String line = null;
  20.                 //按行读取
  21.                 while((line = br.readLine())!=null)
  22.                         al.add(line);  //存入集合
  23.                 //遍历集合
  24.                 for(String s:al){
  25.                         System.out.println(s);
  26.                 }
  27.                 //释放资源
  28.                 br.close();
  29.         }
  30.                
  31. }
复制代码







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2