本帖最后由 skill20 于 2014-4-28 10:30 编辑
- import java.io.*;
- class BufferedTest
- {
- public static void main(String[] args)
- {
- FileReader fr = null;
- try
- {
- fr = new FileReader("BufferTest.java");
- MyBuffer mb = new MyBuffer(fr);
- String st = null;
- while ((st = mb.myRead()) != null)
- {
- System.out.println(st);
- }
-
- }
- catch (IOException e)
- {
- throw new RuntimeException("shiba");
- }
- finally
- {
- try
- {
- if( fr != null)
- fr.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException("cuowu");
- }
- }
- }
- }
- class MyBuffer
- {
- /* public String myRead_1(FileReader r)
- {
- StringBuilder sb = new StringBuilder();
- int num = 0;
- while ((num = r.read()) != -1)
- {
- if(num == '\r')
- continue;
- if(num == '\n')
- return sb.toString();
- else
- sb.append((char)num);
- }
- if(sb.length() != 0)
- return sb.toString();
- return null;
- } */
- private FileReader r;
- MyBuffer(FileReader r)
- {
- this.r = r;
- }
- public String myRead( )throws IOException
- {
- StringBuilder sb = new StringBuilder();
- int num = 0;
- while ((num = r.read()) != -1)
- {
- if(num == '\r')
- continue;
- if(num == '\n')
- return sb.toString();
- else
- sb.append((char)num);
- }
- if(sb.length() != 0)
- return sb.toString();
- return null;
-
- }
- public void close() throws IOException
- {
- r.close();
- }
- }
复制代码
提问:把装饰的类放方法里去,这个过程可行不?
|
|