本帖最后由 涐扪①起奮乧 于 2013-11-10 13:35 编辑
- <P>import java.io.*;
- class MyBufferedReader
- {
- private FileReader r;
- MyBufferedReader(FileReader r)
- {
- this.r = r;
- }
- //可以一次读取一行数据的方法
- public String myReadLine()throws IOException
- {
- //定义一个临时容器。原BufferedReader封装的是字符数组
- //定义一个StringBuilder容器,因为最终还是要将数据变成字符串
- 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;
- }</P>
- <P> public void myClose() throws IOException
- {
- r.close();
- }
- }
- class MyBufferedReaderDemo
- {
- public static void main(String[] args) throws IOException
- {
- FileReader fr = new FileReader("demo.text");
- MyBufferedReader myBuf = new MyBufferedReader(fr);
- String line = null;
- while((line=myBuf.myReadLine())!=null)
- {
- System.out.println(line);
- }
- myBuf.myClose();
- }
- </P>
复制代码 我做练习时写的代码,运行的时候提示,无法加载主类,怎么个情况?
还有 if(sb.length()!=0)
return sb.toString
对这两句代码不太理解 |
|