- import java.io.*;
- class MyBufferedReader
- {
- private FileReader r;
- MyBufferedReader (FileReader f)//f和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 MyBufferedReaderDemo2
- {
- public static void main(String[] args) throws IOException
- {
- FileReader fr = new FileReader ("buf.txt");
- MyBufferedReader mybf = new MyBufferedReader (fr);
-
- String line = null;
- while ((line = mybf.myReadLine()) != null)
- {
- System.out.println(line);
- }
- mybf.myClose();
-
- }
- }
复制代码 |