以下程序运行结果怎么会是null啊?asdf.txt文件里边明明有内容啊- import java.io.*;
- class MyBufferedReader
- {
- private FileReader r;
- MyBufferedReader(FileReader r)
- {
- this.r = r;
- }
- public String myReadLine()throws IOException
- {
- StringBuilder sb = new StringBuilder();
- 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;
- }
- public void myClose()throws IOException
- {
- r.close();
- }
- }
- class MyBufferedReaderDemo
- {
- public static void main(String[] args)throws IOException
- {
- FileReader fr = new FileReader("asdf.txt");
- MyBufferedReader myBuf = new MyBufferedReader(fr);
- String line = null;
- while((line=myBuf.myReadLine())!=null);
- {
- System.out.println(line);
- }
- myBuf.myClose();
- }
- }
复制代码 |