自己写的LineNumberReader,可以运行,但是是无限循环,找不出错误在哪儿
import java.io.*;
class MyLineNumberReader
{
private Reader r;
private int ln;
MyLineNumberReader(Reader r)
{
this.r=r;
}
public void setLineNumber(int ln)
{
this.ln=ln;
}
public int getLineNumber()
{
return ln;
}
public String myReadLine() throws IOException
{
StringBuilder sb=new StringBuilder();
int ch=0;
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();
}
}
class MyLineNumberReaderDemo
{
public static void main(String[] args) throws IOException
{
FileReader fr=new FileReader("BufferedReaderDemo.java");
MyLineNumberReader mlnr=new MyLineNumberReader(fr);
String s=null;
while ((s=mlnr.myReadLine())!=null)
{
System.out.println(s);
}
mlnr.myClose();
}
}