Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.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 Eclipse朱诺:未分配的可关闭值_Java_Eclipse - Fatal编程技术网

Java Eclipse朱诺:未分配的可关闭值

Java Eclipse朱诺:未分配的可关闭值,java,eclipse,Java,Eclipse,我想知道为什么我在新的EclipseJuno中得到了这个警告,尽管我认为我正确地关闭了所有东西。你能告诉我为什么我在下面的代码中得到这个警告吗 public static boolean copyFile(String fileSource, String fileDestination) { try { // Create channel on the source (the line below generates a warning unassigned c

我想知道为什么我在新的EclipseJuno中得到了这个警告,尽管我认为我正确地关闭了所有东西。你能告诉我为什么我在下面的代码中得到这个警告吗

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;
    } 
 }

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

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


注意:即使您在
catch
块中输入了一个返回值,
finally
块也将完成。

eclipse警告您不能再引用
FileInputStream
FileOutputStream

如果您在Java 7上运行,您可以使用新的try和类似的资源块,您的流将自动关闭:

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,则应使用繁琐的旧方法编写代码,最后使用以下代码块:

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版本有多好:)

有了这个解决方案,我得到了一个未处理的IOexception!这是可行的,但我现在想知道如何在不使用此功能的情况下删除此警告!为什么不能在参考资料中直接声明FileChannel。编辑:您刚刚回答了我的问题,但为什么不关闭fileChannel?当您关闭流时,它将关闭该频道。您不需要显式地关闭它,我完全没有注意到(对于java7代码),新FileInputStream和OutputStream的声明发生在打开try{}的括号之前。我猜你提到过,称他们为“资源块尝试”。更正此错误后,警告消失。爱死它了!