a- package demo.io;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.Reader;
- public class IODemo4 {
- /**
- * 实现LineNumberReader
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- File srcFile = new File("c:\\Java review", "Test7.java");
- MyLineNumberReader fr = new MyLineNumberReader(new FileReader(srcFile));
- String line = null;
- while((line = fr.readLine())!= null){
- System.out.println(line);
- }
- fr.close();
- }
- }
- class MyLineNumberReader extends BufferedReader{
-
- public MyLineNumberReader(Reader in) {
- super(in);
- }
- private int line = 0;
- public int getLine() {
- return line;
- }
- public void setLine(int line) {
- this.line = line;
- }
- public String readLine(){
- line++;
- try {
- String str = super.readLine();
- if(str != null)
- return line +" "+ str;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
-
- }
复制代码
|
|