- import java.io.*;
- class TestMybuffered
- {
- public static void main(String[] args) throws IOException
- {
- MyBufferedReader mbr = new MyBufferedReader(new FileReader("Fileread.java"));
- String line=null;
- while((line=mbr.readLine())!=null)
- System.out.println(line);
- mbr.myclose();
- System.out.println("Hello World!");
- }
- }
- class MyBufferedReader
- {
- private Reader r;
- MyBufferedReader(Reader r)
- {
- this.r=r;
- }
- public String readLine() throws IOException
- {
- StringBuilder sb= new StringBuilder();
- int ch=0;
- while((ch=this.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
- {
- this.r.close();
- }
- }
复制代码
这是我自己写的一个MyBufferedReader类,在循环读取的时候,ch=this.r.read() 中this应不应该加啊?我看老师的视频中好像没有这个this!
|
|