本帖最后由 psljkljfslc 于 2009-12-06 01:24:39 编辑

解决方案 »

  1.   

    可以的,比如:
    public class Test {  public static void main(String[] args) throws Exception {
        Test t = new Test();
        byte[] bytes = "ABCD".getBytes();
        t.foo(new ByteArrayInputStream(bytes));
        t.foo(new BufferedInputStream(new ByteArrayInputStream(bytes)));
        t.foo(new DataInputStream(new ByteArrayInputStream(bytes)));
      }  public <T extends InputStream> void foo(T input) throws Exception {
        byte[] buff = new byte[4096];
        int len;
        while ((len = input.read(buff)) != -1) {
          System.out.println(new String(buff, 0, len));
        }
      }
    }但是,就你的例子而言,只要用接口InputStream作为参数就可以了。