- import java.io.*;
 
 - class MyBufferRead
 
 - {
 
 -         private FileReader fr;
 
 -         MyBufferRead(FileReader fr)
 
 -         {
 
 -                 this.fr=fr;
 
 -         }
 
  
-         public String myReadLine() throws IOException
 
 -         {
 
 -                 /*
 
 -                 我们知道在缓冲区中readLine的方法返回是一行的字符串,内存模拟可以通过数组去
 
 -                 接受,并且返回,但是这样不好控制,所以我们直接用StringBuilder来完成,这种动态的
 
 -                 返回字符串,非常便捷,而且简单,所以这里就定义了一个容器
 
 -                 */
 
 -                 StringBuilder sb=new StringBuilder();
 
 -                 
 
 -                 //通过读取单个字符的方式去进行一一读取
 
 -                 int ch=0;
 
  
-                 //判断的条件就是当不为-1时,仍然循环去读取
 
 -                 while ((ch=fr.read())!=-1)
 
 -                 {
 
 -                         if (ch=='\r') //当读取到回车是跳过此循环继续进行下一个循环
 
 -                         {
 
 -                                 continue;
 
 -                         }
 
 -                         if (ch=='\n')//当读取到换行的时候,那么就返回容器中已经存在内容(即为一行)
 
 -                         {
 
 -                                 return sb.toString();
 
 -                         }else
 
 -                         {
 
 -                                 sb.append((char)ch); //把读取的一行数据添加到容器中临时存起来
 
 -                         }
 
 -                 }
 
 -                         if (sb.length()!=0) //最后如果容器中内容长度不为0,那么全部返回
 
 -                         {
 
 -                                 return sb.toString();
 
 -                         }
 
 -                         return null; //否则返回为null
 
 -         }
 
  
-         public void myClose() throws IOException //模仿的是关闭缓冲区流的方法
 
 -         {
 
 -                 fr.close();
 
 -         }
 
 - }
 
 - class MyBufferedReader4 
 
 - {
 
 -         public static void main(String[] args) throws IOException
 
 -         {
 
 -                 //下面的内容很简单了,就是调用自己的方法去执行,最终把读取出来的内容显示在屏幕上
 
 -                 FileReader fr=new FileReader("MyBufferedReader4.java");
 
  
-                 MyBufferRead mr=new MyBufferRead(fr);
 
 -                 String line=null;
 
 -                 while ((line=mr.myReadLine())!=null)
 
 -                 {
 
 -                         sop(line);
 
 -                 }
 
 -                 mr.myClose(); //关闭缓冲区流对象
 
 -         }
 
 -         public static void sop(Object obj)
 
 -         {
 
 -                 System.out.println(obj);
 
 -         }
 
 - }
 
 
  复制代码 
 
关于这里的 
if (sb.length()!=0) //最后如果容器中内容长度不为0,那么全部返回 
        { 
         return sb.toString(); 
        } |   
        
 
    
    
    
     
 
 |