1 package 文件操作;
2
3 import java.io.File;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.io.Reader;
7
8 public class TestReader {
9 public static void main(String[] args) throws IOException {
10 File file=new File("D:"+File.separator+"test.txt");
11 /*File*/Reader reader=new FileReader(file);//也可以直接用FileReader类型的变量接收实例化对象,而无需使用向上转型。
12 if(file.exists()){
13 char[] chars=new char[1024];
14 // int length=reader.read(chars);//把file里的内容读到chars里,一次性读取。
15 int foot=0;
16 int tmp=0;
17 while((tmp=reader.read())!=-1)
18 chars[foot++]=(char)tmp;
19 reader.close();
20 // System.out.println("【"+new String(chars,0,length)+"】");//一次性读取后打印出
21 System.out.println("【"+new String(chars,0,foot)+"】");
22 }
23 }
24 }