标签:ret buffered auth line reader port nts 错误 main
import java.io.FileReader;/**
*/
public class Demo {
public static void main(String[] args) {
MyBufferedReader my = null;
try {
FileReader fr = new FileReader("c:\字符流缓冲区.txt");
my = new MyBufferedReader(fr);
String buff = null;
while((buff = my.myReadLine())!=null){
System.out.println(buff);
}
} catch (Exception e) {
throw new RuntimeException("读取错误");
}finally {
try {
if(my!=null)
my.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class MyBufferedReader extends Reader {
Reader in = null;
public MyBufferedReader(Reader in) {
this.in = in;
}
//增强的方法
public String myReadLine() throws IOException{
StringBuffer sb = new StringBuffer();
int ch = 0;
while((ch = in.read())!=-1){
if(ch == ‘\r‘)
continue;
if(ch== ‘\n‘)
return sb.toString();
else
sb.append((char)ch);
}
if(sb.length()!=0){
return sb.toString();
}
return null;br/>}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
return in.read(cbuf, off, len);br/>}
@Override
public void close() throws IOException {
in.close();
}
}
标签:ret buffered auth line reader port nts 错误 main
原文地址:http://blog.51cto.com/13579086/2064987