有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java文件OutputStream的存在会导致InputStream。读取错误

我对Java IO有一些问题,下面的代码不起作用,变量计数直接返回-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,在Windows7中使用Google插件和Java6.21。 PS:我更改了原始代码,但这不会影响问题


共 (0) 个答案