- package IO;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.LineNumberReader;
- class MyBufferedLineNumberReader extends LineNumberReader
- {
- private LineNumberReader lb;
-
- public MyBufferedLineNumberReader(FileReader fr,LineNumberReader lb)
- {
- super(fr);
- this.lb = lb;
- }
-
- public String MyReadLine() throws IOException
- {
- StringBuilder sb = new StringBuilder();
- int ch = 0;
- while((ch = lb.read())!= -1)
- {
- if(ch == '\r')
- {
- continue;
- }
- if(ch == '\n')
- {
- return sb.toString();
- }
- else
- {
- sb.append((char)ch);
- }
- }
- if(sb.length()!=0)
- {
- return sb.toString();
- }
- return null;
- }
- }
- public class MyBufferedLineNumberReaderDemo {
- public static void main(String[] args) throws IOException {
- FileReader fr = new FileReader("E:\\JAVA\\java视频\\黑马程序员JAVA基础\\19\\ReaderDemocopy.txt");
- LineNumberReader lnr = new LineNumberReader(fr);
- MyBufferedLineNumberReader mb = new MyBufferedLineNumberReader(fr, lnr);
-
-
- String str = null;
-
- while((str = mb.MyReadLine())!= null){
- System.out.println("line:"+mb.getLineNumber()+":"+str);
- }
- mb.close();
- }
- }
复制代码
问题1:装饰类继承了一个没有无参构造的类,我该这样做么?还是有别的方法请不吝指教!
问题2:为什么打印出来的getLineNumber全是0?C:\Users\yy\Desktop |