字符流装饰类,用户在每行代码前加行号
- public class MyLineNumberReader
- {
- private Reader r;
- //private StringBuffer sb = new StringBuffer();
- private int number;
- MyLineNumberReader(Reader r)
- {
- this.r = r;
- }
- public String readerLine()throws Exception
- {
- number++;
- StringBuffer sb = new StringBuffer();
- //sb.append(number+":"); //为什么打开这里,创建字符缓冲区后,在每行前添加行号就不行了呢?注释掉就可以每行打印出来
- int ch = 0;
- while((ch=r.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;
- }
-
- }
复制代码
|
|