guwenren
package itcast.java16;

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

/*
 * 
 * (文本文件读取方式一)
 */
public class FileReaderDemo1 {
    public static void main(String[] args) {
        // 创建一个文件取流对象,和指定名称的文件相关联。
        // 要保证该文件是已经存在的,如果不存在,会发生异常FileNotFoundException
        FileReader fr = null;
        try {
            fr = new FileReader("demo.txt");
            int i = 0;
            // 调用读取流方式对象的read方法
            // read()一次读一个字符。而且会自动往下读。
            while ((i = fr.read()) != -1) {
                System.out.println((char) i);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (fr != null)
                try {
                    fr.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }

    }
}

 

分类:

技术点:

相关文章: