FileReader
- 文件字符输入流,只能读取普通文本。
- 读取文本内容时,比较方便,快捷。
构造方法
- FileReader(String fileName):在给定从中读取数据的文件名的情况下创建一个新FileReader。
- FileReader(File file):在给定从中读取数据的File的情况下创建一个新FileReader。
使用方法:
public class FileReaderTest {
public static void main(String[] args) {
FileReader reader = null;
try {
//创建文件字符输入流
//tempfile是项目中文件的名字
reader = new FileReader("tempfile");
//开始读
char[] chars = new char[4]; //一次读取4个字符
int readCount = 0;
while((readCount = reader.read(chars)) != -1) {
System.out.print(new String(chars, 0, readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}