美文网首页
2021-08-27 IO流(文本文件读取方式一)

2021-08-27 IO流(文本文件读取方式一)

作者: Denholm | 来源:发表于2021-09-12 10:34 被阅读0次
import java.io.FileReader;
import java.io.IOException;

public class FileReaderDemo {

    public static void main(String[] args) throws IOException {
        // 创建一个文件读取流对象,和指定名称的文件相关联
        // 要保证该文件是已经存在的;否则会发生FileNotFoundException
        FileReader reader = new FileReader("E:/demo.txt");

        // 调用读取流对象的read方法读取文件
        // read():一次读取一个字符。而且会自动往下读
        int ch;
        while ((ch = reader.read()) != -1) {
            System.out.print((char) ch);
        }

        reader.close();
    }

}
读取文件示意图

相关文章

网友评论

      本文标题:2021-08-27 IO流(文本文件读取方式一)

      本文链接:https://www.haomeiwen.com/subject/nkcviltx.html