实现BufferedReader方法

package privateclass;

import java.io.Closeable;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class MyBufferedReader {

	private FileReader reader;
	private int count = 0;
	private int pos = 0;
	private char []buf = new char[1024];
	
	public MyBufferedReader(FileReader reader) {
		this.reader = reader;
	}
	public int Myread() throws IOException {
		if(count == 0)
		{
			count = reader.read(buf);
			pos = 0;
		}
		if(count < 0)return -1;
		char ch = buf[pos];
		pos++;
		count--;
		return ch;
	}
	public String Myreadline() throws IOException {
		StringBuilder s = new StringBuilder();
		int ch = 0;
		while((ch = Myread()) != -1)
		{
			if(ch == '\r')continue;
			if(ch == '\n')return s.toString();
			s.append((char)ch);
		}
		/*
		 * 在这里是因为怕读不到回车
		 */
		if(s.length() != 0)
		return s.toString();
		return null;
	}
	public void close() throws IOException {
		reader.close();
	}
}

  

posted @ 2019-11-03 20:09  WINDZLY  阅读(254)  评论(0编辑  收藏  举报