首页 > 其他 > 详细

FileReader

时间:2015-11-27 17:29:14      阅读:314      评论:0      收藏:0      [点我收藏+]

一、使用步骤:

  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();
    }

 

FileReader

原文:http://www.cnblogs.com/lyjs/p/5001004.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!