LineNumberReader的实现

import java.io.*;

public class MyLineNumberReader extends BufferedReader
{
	private int line = 0;  //行号
	public MyLineNumberReader(Reader r)
	{
		super(r);
	}
	
	public void setLineNumber(int lineNumber)
	{
		this.line = lineNumber;
	}
	
	public String myReadLine() throws IOException
	{
		line++;
		return super.readLine();
	}
	
	public int getLine()
	{
		return line;
	}
	
	public static void main(String[] args) throws IOException {
		FileReader fr = new FileReader("buf_copy.txt");
		
		MyLineNumberReader mlnr = new MyLineNumberReader(fr);
		
		String line = null;
		mlnr.setLineNumber(2);
		while((line = mlnr.myReadLine()) != null)
		{
			System.out.println(mlnr.getLine() + ":" + line);
		}
		
		mlnr.close();
	}
}

posted @ 2018-07-19 14:55  cpluspluser  阅读(471)  评论(0编辑  收藏  举报