编写一个类,增强java.io.BufferedReader的ReadLine()方法,使之在读取某个文件时,能打印出行号。
class Myreader extends BufferedReader{
public Myreader(Reader in) {
super(in);
// TODO Auto-generated constructor stub
}
int lineNumber=0;
@Override
public String readLine() throws IOException {
// TODO Auto-generated method stub
String s=super.readLine();
if(s==null)
return s;
else{
return ++lineNumber+"---"+s;
}
}
}
public class Test1 implements Serializable{
public static void main(String[] args) throws Exception {
File f=new File(System.getProperty("user.dir")+"\\a.txt");
if(f.exists())
f.createNewFile();
FileReader fr=new FileReader(f);
Myreader myreader=new Myreader(fr);
String string="";
while((string=myreader.readLine())!=null)
{
System.out.println(string);
}
}
}
|
|