日期:2014-05-20 浏览次数:20986 次
package Buffered;
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 f){
this.fr = f;
}
public int myRead() throws IOException{
if(count == 0){
count = fr.read(buf);
pos = 0;
}
if(count<0)
return -1;
int ch = buf[pos++];
count--;
return ch;
}
public String myReadLine() throws IOException{
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);
}
return null;
}
public void myClose() throws IOException {
fr.close();
}
}
package IOtest;
import java.io.FileReader;
import java.io.IOException;
import Buffered.MyBufferedReader;
public class MyBufferedReaderTest {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("C:\\demo.txt");
MyBufferedReader mbw = new MyBufferedReader(fr);
String line = null;
while((line = mbw.myReadLine()) != null){
System.out.println(line);
}
mbw.myClose();
}
}