java不记:自定义文本复制操作

/*

 

缓冲区的原理:
* 就是从源中获取一批数据装进缓冲区中,
* 在从缓冲区中不断的取出一个一的数据
* 在此次取完后,在从源中获取一批数据进缓冲区
* 当源中的数据取光时,用-1作为结束标志

 

*/

 

import java.io.FileReader;
import java.io.IOException;

public MyBufferedReader{  

           private FileReader r;
     private char[] buff = new char[1024];//定义一个数组作为缓冲区
   private int pos = 0; //定义一个指针作为操作数组中的元素,当操作到最后一个时,指针归零
   private int count = 0; //定义一个计数器,用于记录缓冲区中的数据个数,当该数据个数减为零时,再从源中获取数据到缓冲区中

           public MyBufferedReader(FileReader r){
          this.r = r;
   }
    public int Myread() throws IOException{
         if(count==0){
         count = r.read(buff);
        pos=0;
}
        if(count<0){
            return -1;
       }
     char ch = buff[pos++];
      count--;
    return ch;

 

    public String MyreadLine() throws IOException{

     StringBuilder sb = new StringBuilder();
     int ch = 0;
    while((ch=Myread())!=-1){
  if(ch=='\r')
  continue;
  if(ch=='\n')
  return sb.toString();
  sb.append((char)ch);
  }
  if(sb.length()!=0){
    return sb.toString();
  }
    return null;
}

public void Mycolse() throws IOException{
r.close();
}

}

 

 

posted @ 2020-10-06 21:58  鑫怜彦  阅读(137)  评论(0)    收藏  举报