1,相关方法:(FileReader类)数据输入流

read:
1. **在读取一个字符时,返回的值是一个已读取数据的数据对应字节码;**1. **在读取多个字符时,返回值是一个已读取数据的数量;**
i,字符流输入的步骤:
创建字符流输入对象:
FileReader fr = new FileReader("G:\\abc.txt");
循环读取一个字符:
public class Text {public static void main(String[] args) throws IOException {FileReader fr = new FileReader("G:\\abc.txt");byte[] bytes = new byte[1024 * 8];int len;while ((len = fr.read()) != -1){System.out.print((char) len);}fr.close();}}
循环读取多个字符到数组再输出:(推荐)
注意:这里的数组是char类型的;字符嘛,输出要转换为String(使用String方法) ```java public class Text { public static void main(String[] args) throws IOException {
FileReader fr = new FileReader(“G:\abc.txt”);
//这里的数组是char类型 char[] bytes = new char[1024 * 8]; int len; while ((len = fr.read(bytes)) != -1){ //这里是int转字符;记得用String方法;字符底层是机器码 System.out.println(new String(bytes,0,len)); } fr.close();
} } ```
关闭流:
fr.close();
