【java】io流之字符输入流:java.io.Reader类及子类的子类java.io.FileReader

时间:2023-03-08 21:37:33
 package 文件操作;

 import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader; public class TestReader {
public static void main(String[] args) throws IOException {
File file=new File("D:"+File.separator+"test.txt");
/*File*/Reader reader=new FileReader(file);//也可以直接用FileReader类型的变量接收实例化对象,而无需使用向上转型。
if(file.exists()){
char[] chars=new char[1024];
// int length=reader.read(chars);//把file里的内容读到chars里,一次性读取。
int foot=0;
int tmp=0;
while((tmp=reader.read())!=-1)
chars[foot++]=(char)tmp;
reader.close();
// System.out.println("【"+new String(chars,0,length)+"】");//一次性读取后打印出
System.out.println("【"+new String(chars,0,foot)+"】");
}
}
}