黑马程序员-JAVA高级视频_IO输入与输出-18天-6(文本文件读取方式二)

package itcast.java16;

import java.io.FileReader;
import java.io.IOException;

/*
 * (字符数组读取方式二)
 */
public class FileReaderDemo2 {
    public static void main(String[] args) {
        FileReader fr = null;
        try {
            fr = new FileReader("demo.txt");
            char[] chars = new char[1024];
            int i = 0;
            while ((i = fr.read(chars)) != -1) {
                System.out.println(String.valueOf(chars, 0, i));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null)
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}

 

posted @ 2013-03-21 20:11  谷文仁  阅读(115)  评论(0编辑  收藏  举报