【问题标题】:Eclipse Juno : unassigned closeable valueEclipse Juno:未分配的可关闭值
【发布时间】:2012-08-07 07:17:00
【问题描述】:

我想知道为什么我会在新的 eclipse Juno 中收到此警告,尽管我认为我正确关闭了所有内容。你能告诉我为什么我在下面的代码中收到这个警告吗?

public static boolean copyFile(String fileSource, String fileDestination)
{
    try
    {
        // Create channel on the source (the line below generates a warning unassigned closeable value) 
        FileChannel srcChannel = new FileInputStream(fileSource).getChannel(); 

        // Create channel on the destination (the line below generates a warning unassigned closeable value)
        FileChannel dstChannel = new FileOutputStream(fileDestination).getChannel();

        // Copy file contents from source to destination
        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());

        // Close the channels
        srcChannel.close();
        dstChannel.close();

        return true;
    }
    catch (IOException e)
    {
        return false;
    } 
 }

【问题讨论】:

    标签: java eclipse


    【解决方案1】:

    如果您在 Java 7 上运行,您可以像这样使用新的 try-with-resources 块,您的流将自动关闭:

    public static boolean copyFile(String fileSource, String fileDestination)
    {
        try(
          FileInputStream srcStream = new FileInputStream(fileSource); 
          FileOutputStream dstStream = new FileOutputStream(fileDestination) )
        {
            dstStream.getChannel().transferFrom(srcStream.getChannel(), 0, srcStream.getChannel().size());
            return true;
        }
        catch (IOException e)
        {
            return false;
        } 
    }
    

    您无需显式关闭底层通道。但是,如果您不使用 Java 7,则应该以繁琐的旧方式编写代码,并带有 finally 块:

    public static boolean copyFile(String fileSource, String fileDestination)
    {
        FileInputStream srcStream=null;
        FileOutputStream dstStream=null;
        try {
          srcStream = new FileInputStream(fileSource); 
          dstStream = new FileOutputStream(fileDestination)
          dstStream.getChannel().transferFrom(srcStream.getChannel(), 0, srcStream.getChannel().size());
            return true;
        }
        catch (IOException e)
        {
            return false;
        } finally {
          try { srcStream.close(); } catch (Exception e) {}
          try { dstStream.close(); } catch (Exception e) {}
        }
    }
    

    看看 Java 7 版本好多少:)

    【讨论】:

    • 这可行,但我现在想知道如何在不使用此功能的情况下删除此警告!以及为什么不能在资源中直接声明FileChannel。编辑:您刚刚回答了我的问题,但为什么不关闭 fileChannel?
    • 当你关闭流时,它会关闭通道。您不需要显式关闭它。
    • 我完全错过了(对于 java7 代码)新 FileInputStream 和 OutputStream 的声明发生在为 try{} 打开括号之前。我猜你提到过,称它们为 try-with-resources 块。更正后,警告消失了。喜欢它!
    【解决方案2】:

    您应该始终关闭finally,因为如果出现异常,您将不会关闭资源。

    FileChannel srcChannel = null
    try {
       srcChannel = xxx;
    } finally {
      if (srcChannel != null) {
        srcChannel.close();
      }
    }
    

    注意:即使您在 catch 块中添加了 return,finally 块也会完成。

    【讨论】:

    • 使用该解决方案我得到一个未处理的 IOexception!
    【解决方案3】:

    eclipse 警告您关于 FileInputStreamFileOutputStream 您不能再引用。

    【讨论】:

      猜你喜欢
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多