自定义缓冲区,MyBufferedReader
import java.io.FileReader;
import java.io.IOException;
public class MyBufferedReader {
private FileReader fr;
private char[] buf = new char[1024];
private int count = 0;
private int pos = 0;
public MyBufferedReader(FileReader fr) {
this.fr = fr;
}
public int MyRead() {
try {
if (count == 0) {
count = fr.read();// 记录每次读的字数数
pos = 0;// 读取字符数的角标移位
}
} catch (IOException e) {
e.printStackTrace();
}
if (count < 0)
return -1;
char ch = buf[pos];
count--;
pos++;
return ch;
}
public String MyReadLine() {
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 Myclose() {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |