子类:InputStreamReader
孙子类:FileReader
Reader 类常用方法
int read( ) 从输入流中读取下一个字符,返回0-65535之间的int值.返回值代表着读出来的字符对应的整形数字
int read(char[] c) 一次性获取全部的ASCII 从输入流中读取一堆字符,把这些字节存储在字符数组中。返回值代表着真实的读到的字符的个数(b的长度)
read(char[] c,int off,int len)从输入流中读取一堆字符,把这些字符存储在字符数组中。off对应的是b开始存储字节的标识或下表(将读取的第一个字符存储在元素b[off]中)。len对应的是读到的字符的个数或长度。
void close( )
子类 InputStreamReader 常用的构造方法
InputStreamReader(InputStream in)
InputStreamReader(InputStream in,String charsetName)
FileReader(File file)
FileReader(String name)
该类只能按照本地平台的字符编码来读取数据,
用户不能指定其他的字符编码类型
System.out.println(System.getProperty("file.encoding")); 获得本地平台的字符编码类型
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String[] args) {
FileReader fr = null;
try {
fr = new FileReader("D:/liangsong/hello.txt");
StringBuffer sb = new StringBuffer();
char[] words = new char[1024];
while(fr.read(words) != -1){
sb.append(words);
}
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}