- package cn.it.IO.mybufferedReader;
- import java.io.IOException;
- import java.io.Reader;
- public class MyBufferedReader {
-
- private Reader r;
-
- private char[]buf=new char[1024];
-
- private int index=0;
-
- private int count=0;
-
- public MyBufferedReader(Reader r) {
- super();
- this.r = r;
- }
-
- public int myReader() throws IOException{
-
- if(count==0){
- count =r.read(buf);
- index=0;
-
- }
- if(count<0){
- return -1;
- }
- char ch=buf[index];
- index++;
- count--;
- return ch;
- }
-
- public String myReaderLine() throws IOException{
-
- StringBuilder sb=new StringBuilder();
-
- int ch=0;
-
- while ((ch=myReader())!=-1) {
-
- if(ch=='\r'){
- continue;
- }
- if(ch=='\n'){
- return sb.toString();
- }
- sb.append((char)ch);
- }
-
- if(sb.toString()!=null){//这个地方判断sb里面有无内容怎么不行?
- return sb.toString();
- }
- return null;
- }
-
- public void myClose() throws IOException{
-
- r.close();
- }
- }
复制代码 请看最后注释的地方
|
|