1

我的 Java IO 有问题,下面这段代码不起作用,变量 count 直接返回 -1。

public void putFile(String name, InputStream is) {      

    try {

        OutputStream output = new FileOutputStream("D:\\TEMP\\" + name);

        byte[] buf = new byte[1024];
        int count = is.read(buf);
        while( count >0) {
            output.write(buf, 0, count);
            count = is.read(buf);
        }

    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }

}

但是,如果我评论了 OutputStream,例如

    public void putFile(String name, InputStream is) {      

    try {

        //OutputStream output = new FileOutputStream("D:\\TEMP\\" + name);

        byte[] buf = new byte[1024];
        int count = is.read(buf);
        while( count >0) {
            //output.write(buf, 0, count);
            count = is.read(buf);
        }

    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }

}

计数将返回正确的值 (>-1)。这怎么可能 ?这是一个错误吗?

我在 Eclipse 中使用 Jetty,在 Windows 7 中使用 Google 插件和 Java 6.21。 PS:我更改了原始代码,但它不影响问题

4

0 回答 0