如题
- import java.io.*;
- class MyLineNumberReader
- {
- private Reader r;
- private int lineNumber;
- MyLineNumberReader(Reader r)
- {
- this.r = r;
- }
- public String myReadLine()throws IOException
- {
- lineNumber++;
- StringBuilder sb = new StringBuilder();
- int ch = 0;
- while((ch=r.read())!=-1)
- {
- if(ch=='\r')
- continue;
- if(ch=='\n')
- {
- //System.out.println(sb.toString());
- return sb.toString();
- }
- else
- sb.append((char)ch);
- }
- if(sb.length()!=0)
- return sb.toString();
- return null;
- }
- public void setLineNumber(int lineNumber)
- {
- this.lineNumber = lineNumber;
- }
- public int getLineNumber()
- {
- return lineNumber;
- }
- public void myClose()throws IOException
- {
- r.close();
- }
- }
- class MyLineNumberReaderDemo
- {
- public static void main(String[] args) throws IOException
- {
- FileReader fr = new FileReader("me.txt");
- MyLineNumberReader mylnr = new MyLineNumberReader(fr);
- String line = null;
- //mylnr.setLineNumber(100);
- //line=mylnr.myReadLine();
- //System.out.println(mylnr.getLineNumber()+"::"+line);
- while((<font color="#ff0000">line=mylnr.myReadLine())!=null</font>) <font color="#ffa500"> </font><font color="#0000ff">这条语句读的时候 当我文本里面只有换行符的时候 是不是不算NULL 那么也就是说流可以存储换行吗?</font>
- {
- System.out.println(mylnr.getLineNumber()+"::"+line);
- }
- mylnr.myClose();
- }
- }
复制代码
|
|