import java.io.*;
class MyBufferedReader
{
private FileReader r =null;
MyBufferedReader(FileReader r)
{
this.r = r;
}
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)//上面的while循环里面已经往StringBuilder里面存了,这里的长度肯定不是0啊,这返回的应该是之前存入的所有字符啊
//为什么返回的的是 上次返回,到这次之间的字符呢
return sb.toString();
return null;
}
public void myClose() throws IOException
{
r.close();
}
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("buf.txt");
MyBufferedReader mbr = new MyBufferedReader(fr);
String line =null;
while ((line =mbr.myReadLine())!=null)
{
System.out.println(line);
}
mbr.myClose();
}
}
|