黑马程序员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();
                }
        }
    }
}
原文地址:https://www.cnblogs.com/guwenren/p/2974123.html