1 package io;
2
3 import java.io.BufferedReader;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.util.LinkedList;
7 import java.util.List;
8 import java.util.ListIterator;
9
10 public class FileIntoList {
11
12
13 public static List<String>
14 read(String filename) throws IOException {
15 List<String> list = new LinkedList<String>();
16 BufferedReader reader = new BufferedReader(
17 new FileReader(filename)
18 );
19 String s ;
20 while ( (s= reader.readLine())!= null ) {
21 list.add(s);
22 }//使用 readLine() 一般要用BufferedReader
23 reader.close();
24 return list;
25
26 }
27
28
29 public static void main(String[] args) throws IOException {
30 //List<String> list = read("IO1.java"); java.io.FileNotFoundException:
31 List<String> list = read("D:\\dev2\\workspace\\seehope\\bigwork\\src\\io\\IO1.java");
32
33 /*按行倒序输出
34 for(ListIterator<String> it = list.listIterator(list.size());
35 it.hasPrevious(); ) {
36 System.out.println(it.previous());
37 }//for
38 */
39 for(ListIterator<String> it = list.listIterator();
40 it.hasNext(); ) {
41 System.out.println(it.next());
42 }
43 //迭代器输出
44 } //main
45
46
47 }
48
49
50
项目结构
运行