Java_基础—字符流FileReader
package com.soar.chario;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo1_FileReader {
/*
*** 1.字符流是什么
* 字符流是可以直接读写字符的IO流
* 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写出.
*** 2.FileReader
* FileReader类的read()方法可以按照字符大小读取
*
* FileReader fr = new FileReader("aaa.txt"); //创建输入流对象,关联aaa.txt
int ch;
while((ch = fr.read()) != -1) { //将读到的字符赋值给ch
System.out.println((char)ch); //将读到的字符强转后打印
}
fr.close(); //关流
*/
public static void main(String[] args) throws IOException {
//read();
FileReader fr = new FileReader("xxx.txt");
int c;
while((c = fr.read()) != -1){ //通过项目默认的码表一次读取一个字符
System.out.print((char)c);
}
fr.close();
}
private static void read() throws FileNotFoundException, IOException {
FileReader fr = new FileReader("xxx.txt");
int x = fr.read();
System.out.println(x);
//向下强转,将字符拿出
char c = (char) x;
System.out.println(c);
fr.close();
}
}