- import java.io.*;
- class MyBuffer
- {
- private FileReader fr;
-
- MyBuffer(FileReader fr){
- this.fr=fr;
- }
- public String myReadLine()throws Exception{
- StringBuilder sb=new StringBuilder();
- int len=0;
- while((len=fr.read())!=-1){
- if(len=='\r'){
- continue;
- }
- else if(len=='\n'){
- return sb.toString();
- }
- else{
- sb.append((char)len);
- }
- }
-
- if(sb.length()!=0){
- return sb.toString();
- }
- return null;
- }
- public void myClose()throws Exception{
- fr.close();
- }
- }
- class ReadLineDemo
- {
- public static void main(String[] args)throws Exception
- {
- MyBuffer mybur=new MyBuffer(new FileReader("Demo.java"));
- String line=null;
- while((line=mybur.myReadLine())!=null){
- System.out.println(line);
- }
- mybur.myClose();
- }
- }
复制代码 问题是,为什么读取到'\n'的时候,StringBuilder 容器不清空?
求指教。
|
|