Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java资源泄漏_Java_Fileinputstream_Resource Leak - Fatal编程技术网

Java资源泄漏

Java资源泄漏,java,fileinputstream,resource-leak,Java,Fileinputstream,Resource Leak,我的应用程序中有这个代码片段,我很确定我有 关闭了所有的溪流 但是,令人惊讶的是,我一直得到: 已在附加的堆栈跟踪中获取资源,但从未释放。有关避免资源泄漏的信息,请参阅java.io.Closeable。 java.lang.Throwable:未调用显式终止方法“close” 任何指针都会非常有用 if (fd != null) { InputStream fileStream = new FileInputStream(fd.getFileDescriptor()); Byt

我的应用程序中有这个代码片段,我很确定我有 关闭了所有的溪流

但是,令人惊讶的是,我一直得到: 已在附加的堆栈跟踪中获取资源,但从未释放。有关避免资源泄漏的信息,请参阅java.io.Closeable。 java.lang.Throwable:未调用显式终止方法“close”

任何指针都会非常有用

if (fd != null) {
    InputStream fileStream = new FileInputStream(fd.getFileDescriptor());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    try {
        for (int readNum; (readNum = fileStream.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);
        }
        content = bos.toByteArray();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {

            if (fileStream != null) {
                fileStream.close();
            }

            if (bos != null) {
                bos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

尝试将流的实例化移动到
try

InputStream fileStream = null;
ByteArrayOutputStream bos = null;
byte[] buf = new byte[1024];
try {

  fileStream = new FileInputStream(fd.getFileDescriptor());
  bos = new ByteArrayOutputStream();

尝试将流的实例化移动到
try

InputStream fileStream = null;
ByteArrayOutputStream bos = null;
byte[] buf = new byte[1024];
try {

  fileStream = new FileInputStream(fd.getFileDescriptor());
  bos = new ByteArrayOutputStream();

尝试将流的实例化移动到
try

InputStream fileStream = null;
ByteArrayOutputStream bos = null;
byte[] buf = new byte[1024];
try {

  fileStream = new FileInputStream(fd.getFileDescriptor());
  bos = new ByteArrayOutputStream();

尝试将流的实例化移动到
try

InputStream fileStream = null;
ByteArrayOutputStream bos = null;
byte[] buf = new byte[1024];
try {

  fileStream = new FileInputStream(fd.getFileDescriptor());
  bos = new ByteArrayOutputStream();

尝试使用Try-with-resources。这样就无需关闭finally块中的资源


尝试使用Try-with-resources。这样就无需关闭finally块中的资源


尝试使用Try-with-resources。这样就无需关闭finally块中的资源


尝试使用Try-with-resources。这样就无需关闭finally块中的资源


我看到的是,如果关闭文件流时发生异常,bos将不会关闭

如前所述:使用try with resources语句:
我看到的是,如果关闭文件流时发生异常,bos将不会关闭

如前所述:使用try with resources语句:
我看到的是,如果关闭文件流时发生异常,bos将不会关闭

如前所述:使用try with resources语句:
我看到的是,如果关闭文件流时发生异常,bos将不会关闭

如前所述:使用try with resources语句:
使用try with resources可以解决此问题。您可以在这里查看Java 7中引入的自动关闭接口,使用try with resources可以解决此问题。您可以在这里查看Java 7中引入的自动关闭接口,使用try with resources可以解决此问题。您可以在这里查看Java 7中引入的自动关闭接口,使用try with resources可以解决此问题。您可以在此处查看Java 7中引入的AutoCloseable接口。抱歉,我没有将代码添加到问题中。抱歉,我没有将代码添加到问题中。抱歉,我没有将代码添加到问题中。抱歉,我没有将代码添加到问题中。我想如果您使用try with resources会更好,它实现了自动关闭。当然,只有当您使用Java 7或更高版本时,我想如果您使用try with resources(实现自动关闭)会更好。当然,只有当您使用Java 7或更高版本时,我想如果您使用try with resources(实现自动关闭)会更好。当然,只有当您使用Java 7或更高版本时,我想如果您使用try with resources(实现自动关闭)会更好。当然,只有当您使用的是Java7或更高版本时。