import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class MyLineNumberReaderDemo {
public static void main(String[] args) {
MyLineNumberReader mn = null;
try {
mn = new MyLineNumberReader(new FileReader("src/LineNumberReaderDemo.java"));
mn.setLineNumber(10);
for(String line;(line=mn.readLine())!=null;) {
System.out.println(mn.getLineNumber() + " " + line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(mn != null) {
mn.close();
mn = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
class MyLineNumberReader extends BufferedReader{
private int lineNumber;
MyLineNumberReader(Reader r) {
super(r);
}
public String readLine() throws IOException {
lineNumber++;
return super.readLine();
}
public int getLineNumber() {
return lineNumber;
}
public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
}
java 自定义LineNumerReader
最新推荐文章于 2021-02-24 15:29:46 发布