FileReader字符输入流
在给定从中读取数据的文件名的情况下创建一个新 FileReader。
fileName - 要从中读取数据的文件的名称
public FileReader(File file) throws FileNotFoundException
在给定从中读取数据的 File 的情况下创建一个新 FileReader。
file - 要从中读取数据的 File
读取单个字符。
读取的字符,如果已到达流的末尾,则返回 -1
public int read(char[] cbuf) throws IOException
读取一个字符数组
读取的字符,如果已到达流的末尾,则返回 -1
构造方法
public FileReader(String fileName) throws FileNotFoundException在给定从中读取数据的文件名的情况下创建一个新 FileReader。
fileName - 要从中读取数据的文件的名称
public FileReader(File file) throws FileNotFoundException
在给定从中读取数据的 File 的情况下创建一个新 FileReader。
file - 要从中读取数据的 File
常用方法
public int read() throws IOException读取单个字符。
读取的字符,如果已到达流的末尾,则返回 -1
public int read(char[] cbuf) throws IOException
读取一个字符数组
读取的字符,如果已到达流的末尾,则返回 -1
cbuf - 目标缓冲区
public void close() throws IOException
关闭该流并释放与之关联的所有资源。
程序示例
public static void main(String[] args) throws Exception {
//读取当前项目下的StringDemo.java文件
FileReader fr = new FileReader("StringDemo.java")
//一次读取一个字符数组
char[] chs = new char[1024] ;
int len = 0 ;
while((len=fr.read(chs))!=-1) {
System.out.println(new String(chs,0,len));
}
//释放资源
fr.close();
}