FileReader

一、使用步骤:

  1. 找到目标文件
  2. 建立数据的输入通道
  3. 读数据
  4. 关闭输入通道

二、单个字符读取

public static void readTest1() throws IOException{
        //找到目标文件
        File file=new File("E:\\a.txt");
        //建立数据的输入通道
        FileReader fileReader=new FileReader(file);
        int context=0;
        while((context=fileReader.read())!=-1){
            System.out.print((char)context);
        }
        //关闭输入通道
        fileReader.close();
    }

三、使用缓冲数组读取

public static void readTest2() throws IOException{
        //找到目标文件
        File file=new File("E:\\a.txt");
        //建立数据的输入通道
        FileReader fileReader=new FileReader(file);
        //建立缓冲字符数组读取 文件数据
        char[] buf=new char[1024];
        int length=0;
        while((length=fileReader.read(buf))!=-1){
            System.out.print(new String(buf,0,length));
        }
        //关闭输入通道
        fileReader.close();
    }

 

posted @ 2015-11-27 16:15  好人难寻  阅读(162)  评论(0编辑  收藏  举报