要求:
本项目路径下,有一个a.txt文件,内容为"HelloWorld你好世界";
利用 FileReader读取文本内容,并在控制台输出打印;
Test99.java测试类:
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Test99 { public static void main(String[] args) { FileReader fr = null; try { //FileReader下方有红线,点击提示,try...catch fr = new FileReader("a.txt"); //定义有效长度 int len; //定义空的字符数组 //一次读两个字符 char [] cha = new char[2]; //read下方有红线,点击提示,catch //当读到-1时,说明已经读完了 while ((len= fr.read(cha))!=-1){ System.out.println(new String(cha,0,len)); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e)