package cn.itcast.other;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/*
* 编写一个类拓展BufferedReader的功能, 增强readLine方法返回 的字符串带有行号+ 分号。
*
* */
public class Demo1 {
public static void main(String[] args) throws IOException {
File file=new File("Demo1.java");
FileReader fileReader=new FileReader(file);
BufferedReader bufferedReader=new BufferedReader(fileReader);
BufferedHangHao bufferedHangHao=new BufferedHangHao(bufferedReader);
BufferedFengHao bufferedFengHao=new BufferedFengHao(bufferedHangHao);
String line=null;
while((line=bufferedFengHao.readLine())!=null){
System.out.println(line);
}
}
}
//行号类
class BufferedHangHao extends BufferedReader {
BufferedReader bufferedReader;
BufferedHangHao(BufferedReader bufferedReader) {
super(bufferedReader);
this.bufferedReader = bufferedReader;//疑问:如果没有这句话为什么会报错???
}
int count = 1;
public String readLine() throws IOException {
String line = bufferedReader.readLine();
if(line==null){
return null;
}
line = count + "" + line;
count++;
return line;
}
}
//分号类
class BufferedFengHao extends BufferedReader{
BufferedReader bufferedReader;
public BufferedFengHao(BufferedReader bufferedReader) {
super(bufferedReader);
this.bufferedReader = bufferedReader;
}
public String readLine() throws IOException {
String line=bufferedReader.readLine();
if(line==null){
return null;
}
line=line+";";
return line;
}
} |
|