1.
package lzh.inputoutput.test;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
public class TestReader {
public static void main(String[] args) throws Exception {
// 1.定义要输入文件的路径
File file = new File("D:"+File.separator+"demo"+File.separator+"testReader.txt");
// 2.判断文件目录是否存在,存在,则进行读取操作,将文件内容读到内存中
if(file.exists()){
// 3.为Reader类对象实例化
Reader in = new FileReader(file);
// 4.数据读取
char data[] = new char[1024];
int len = in.read(data);
// 5.关闭输入流
in.close();
// 用输出来验证结果
System.out.println(new String(data,0,len));
}else{
System.out.println("文件不存在,请检查指定文件是否存在。");
}
}
}