黑马程序员技术交流社区
标题:
模拟缓冲区
[打印本页]
作者:
李东梁
时间:
2014-4-22 18:46
标题:
模拟缓冲区
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();
}
}
复制代码
请看最后注释的地方
作者:
织语不如书
时间:
2014-4-22 19:05
sb.toString()!=null。。。。。。
判断是否为null判断的是一个引用有没有指向对象,而不是一个确定的对象里有没有东西
对于StringBuilder来讲,如果它本身不是null,那调用toString方法后不可能返回null;如果它本身就是null,那个它根本没法调用toString方法,会抛出空指针异常。
所以这句话完全没有意义……
作者:
osully
时间:
2014-4-22 19:12
你判断 sb 的length 不就行了吗
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2