码迷,mamicode.com
首页 > 其他好文 > 详细

读取文件,按行输出

时间:2018-06-04 22:35:47      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:buffer   读取   turn   into   asn   name   dev   style   image   

 

 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  

 

项目结构  

技术分享图片

 

运行

 技术分享图片

 

读取文件,按行输出

标签:buffer   读取   turn   into   asn   name   dev   style   image   

原文地址:https://www.cnblogs.com/kwaitfort/p/9135689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!