重写readline方法

来源:互联网 发布:java 导出csv文件 编辑:程序博客网 时间:2024/05/01 03:46
package com.xiaorenwu.iotest;import java.io.FileReader;import java.io.IOException;public class ReadLine1 {    //声明一个字符数组当做缓冲区    private char buf[] =new char[1024];    //声明一个字符数组指针用于标记位置    private int pos;    //声明一个计数器用于记录字符数组中的元素个数    private int count;    //声明一个读入文件流。用于接收读入文件流    private FileReader fr;    //构造函数,给变量进行初始化    public ReadLine1(FileReader fr){        this.fr=fr;        pos=0;        count=0;    }    //定义myRead()    public int myRead() throws IOException{        if(count==0){            count=fr.read(buf);            pos=0;            //如果数组中没有元素,从源中取出一批元素,            //存放在数组中,并用count记录其个数            //并重新初始化指针        }        if(count!=-1){            count--;            return buf[pos++];        }else{            return -1;        }    }    public String myReadLine() throws IOException{         StringBuilder sb=new StringBuilder();         int ch=0;         while((ch=myRead())!=-1){             if((char)ch=='\r'){                 continue;             }             if((char)ch=='\n'){                 break;             }             sb.append(ch);         }         return sb.toString();    }}
原创粉丝点击