A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hyz123456 中级黑马   /  2013-10-8 12:52  /  1171 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.*;
class myReaderDemo
{
        public static void main(String[] args) throws IOException
        {
                FileReader fr = new FileReader("demo.java");

                MyBufferReader mbr = new MyBufferReader(fr);

                String line = null;

                while((line=mbr.myReadLine())!=null)
                {
                        System.out.println(line);
                }
                mbr.myClose();
        }
}

class MyBufferReader extends Reader
{
        private Reader r;
        private int ch;
        StringBuffer sb = new StringBuffer();
        MyBufferReader(Reader r)
        {
                this.r = r;
        }
        public String myReadLine()throws IOException
        {
                while ((ch=r.read())!=-1)
                {
                        if (ch=='\r')
                                continue;
                        if(ch=='\n')       
                                return sb.toString();
                        else
                                sb.append((char)ch);
                }
                if (sb.length()!=0)
                        return sb.toString();
                return null;
        }
        public void myClose()throws IOException
        {
                r.close();
        }
        public int read(char[] cbuf, int off, int len) throws IOException
        {
                return r.read(cbuf,off,len) ;
        }

        public void close()throws IOException
        {
                r.close();
        }
}

为什么代码停不下来呢? 哪里出错了啊??

1 个回复

倒序浏览
可以参考java.io.BufferedReader类的readLine()方法   很复杂,就不贴代码了 呵呵
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马