-------------------------------------java中字符流输入流FileReader详解 ----------------------------
Reader‘是一个抽象类,我们需要学习它的孙子类FileReader’
FileReader的爷爷类是Reader
FileReader的构造器
构造方法摘要 |
|
FileReader |
|
FileReader |
读取文件helloworld.txt打印到控制台。
分析:
1. 创建输入流对象。
2. 读取数据
3. 关闭输入流
public class IOCharReaderDemo {
public static void main (String[] args) {
readFile();
}
public static void readFile() {
FileReader fr = null;
try {
//创建FileReader读取对象
//也可以使用这种构造器
// fr = new FileReader(new File("helloword.txt"))
fr = new FileReader("helloworld.txt");
//读取文件也会产生异常,我们需要处理
//直接再加一个catch (IOException e)
//读取单个字符,返回的是字符的ascll码
int num = fr.read();
//打印读取的字符
//num是字符所对应的ASCll码
System.out.println(num);//输出97
//我们可以将它转换从字符
System.out.println((char)num);//输出a
//我们再读取一遍fr