码迷,mamicode.com
首页 > 编程语言 > 详细

Java IO总结之缓冲读入文件

时间:2016-08-23 23:26:16      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

package com.io;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

/**
 * 缓冲读入文件
 * @author Administrator
 *
 */
public class BufferedInputFile {

    public static String read(String filename) throws IOException{
            //reading input by lines:
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String s;
        StringBuilder sb= new StringBuilder();
        
        while((s=in.readLine())!=null){
            sb.append(s+"\n");
        }
        in.close();
        return sb.toString();
    }
    
    //test1,打开一个文本文件,每次读取一行内容,将每行作为一个string读入,并将该string对象存入linklist中,逆序打印linklist
    public static void printLinkList(String fileName) throws IOException{
        BufferedReader in = new BufferedReader(new FileReader("C:/Users/Administrator/Desktop/exec.txt"));
        String string;
        LinkedList<String> list = new LinkedList<String>();
        while((string=in.readLine())!=null){
            list.add(string);
        }
        /*for(int i=list.size();i>=0;i--){
            System.out.println(list.get(i));
        }*/
        java.util.ListIterator<String> item = list.listIterator(list.size());
        
        while(item.hasPrevious()){
            System.out.println(item.previous());
        }
        
        while(item.hasNext()){
            System.out.println(item.next());
        }
        
        /**
         * 结果:
         *  4-->4-->4:4:4:ghdgml11
            3-->3-->3:3:3:flypiger
            2-->2-->2:2:2:ghdgml11
            1-->1-->1:1:1:flypiger
         */
        
    }
    
    public static void main(String[] args) throws IOException {
        //System.out.println(read("C:/Users/Administrator/Desktop/exec.txt"));
        printLinkList("C:/Users/Administrator/Desktop/exec.txt");
    }
}

不要问为什么,代码诠释一切

Java IO总结之缓冲读入文件

标签:

原文地址:http://www.cnblogs.com/tingbogiu/p/5801003.html

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