- 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;
- else 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)
- {
- FileReader fr = null;
- try
- {
- fr = new FileReader("demo.txt");
- MyBufferedReader myBuf = new MyBufferedReader(fr);
- String line = null;
- while((line = myBuf.myReadLine())!=null)
- {
- System.out.println(line);
- }
- }
- catch (IOException e)
- {
- throw new RuntimeException("fail to read");
- }
- finally
- {
- try
- {
- if(fr != null)
- myBuf.myClose();
- }
- catch (IOException e)
- {
- throw new RuntimeException("fail to close");
- }
- }
- }
- }
复制代码 |