From c743b79d734e96d8a8a66865fc9a0966d88da7a8 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Fri, 23 Oct 2026 11:34:33 +0800 Subject: [PATCH] =?UTF-8?q?s-day09--=E5=AD=97=E7=AC=A6=E6=B5=81=E7=9A=84?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E4=B8=80=E4=B8=AA=E5=AD=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- s-day09/src/com/inmind/reader03/Demo10.java | 46 +++++++++++++++++++++ source2.txt | 1 + 2 files changed, 47 insertions(+) create mode 100644 s-day09/src/com/inmind/reader03/Demo10.java create mode 100644 source2.txt diff --git a/s-day09/src/com/inmind/reader03/Demo10.java b/s-day09/src/com/inmind/reader03/Demo10.java new file mode 100644 index 0000000..ea8f6bd --- /dev/null +++ b/s-day09/src/com/inmind/reader03/Demo10.java @@ -0,0 +1,46 @@ +package com.inmind.reader03; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; + +/* +12.字符流的读取一个字符 +在java使用一个抽象父类Reader表示字符输入流 +常用子类:FileReader +构造方法: +FileReader(File file) 创建一个新的 FileReader ,给出 File读取。 +FileReader(String fileName) 创建一个新的 FileReader ,给定要读取的文件的名称。 + +常用方法: +abstract void close() 关闭资源 + +int read() 读一个字符,返回了读取到的字符数据 +int read(char[] cbuf) 将字符读入数组。 + + +使用步骤: + 1.创建对象 + 2.调用读方法 + 3.释放资源 + */ +public class Demo10 { + public static void main(String[] args) throws IOException { + //1.创建对象 + FileReader fr = new FileReader("source2.txt"); + //2.调用读方法 + //int read() 读一个字符,返回了读取到的字符数据 + /*int c = fr.read(); + System.out.println((char) c); + + c = fr.read(); + System.out.println((char) c);*/ + + int c;//用来保存读取到的字符 + while ((c = fr.read()) != -1) { + System.out.println((char) c); + } + //3.释放资源 + fr.close(); + } +} diff --git a/source2.txt b/source2.txt new file mode 100644 index 0000000..684fcff --- /dev/null +++ b/source2.txt @@ -0,0 +1 @@ +abc中国我是谁我在哪我在干嘛! \ No newline at end of file