- //模拟BufferedReader
- package buffer;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- class MyBufferedReader {
- private FileReader r;
- MyBufferedReader(FileReader r)
- {
- this.r = r;
- }
- //可以一次读一行的方法
- public String myReadLine() throws IOException
- {
- //定义一个临时容器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;
- }
- public void myClose() throws IOException
- {
- r.close();
- }
- }
- class MyBufferedReaderDemo
- {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- FileReader fr = new FileReader("demo.txt");
- MyBufferedReader myBuf = new MyBufferedReader(fr);
- String line = null;
- while((line = myBuf.myReadLine())!=null)
- {
- System.out.println(line);
- }
- myBuf.myClose();
- }
- }
复制代码 为什么代码在EditPlus里可以把demo.txt的文件读出来,而在Eclipse里面不行?什么原因
|
|