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