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();
}
}
为什么代码停不下来呢? 哪里出错了啊??
|
|