【问题标题】:error while using try with resources in Java在 Java 中对资源使用 try 时出错
【发布时间】:2013-11-22 20:54:43
【问题描述】:

我有这种方法,我正在使用 Java SE 7 的资源进行尝试。

private void generateSecretWord(String filename){

        try (FileReader files = new FileReader(filename)){
            Scanner input = new Scanner(files);
            String line = input.nextLine();
            String[] words = line.split(",");
            Collections.shuffle(Arrays.asList(words));
            if (words[0].length()>1){
                secretWord = words[0];
                return;
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        finally {
            if (files!=null) files.close(); 
        }

    }

我在 finally 块中遇到编译错误 files cannot be resolved to a variable 我有 try with block 中的文件参考。为什么会出现此错误以及如何解决?

谢谢

【问题讨论】:

    标签: java try-catch-finally finally try-with-resources


    【解决方案1】:

    当您对资源使用 try 时,您不需要显式关闭它们。 try-with-resources 将负责关闭这些资源。

    基于try-wtih-resource document

    try-with-resources 语句是声明一个或多个资源的 try 语句。资源是程序完成后必须关闭的对象。 try-with-resources 语句确保每个资源在语句结束时关闭。

    【讨论】:

      【解决方案2】:

      取自Java Language Spec (14.20.3):

      try-with-resources 语句使用变量(称为资源)进行参数化,这些变量在执行 try 块之前被初始化,并在执行 try 块后以与初始化相反的顺序自动关闭。当资源自动关闭时,catch 子句和 finally 子句通常是不必要的

      ResourceSpecification 使用初始化表达式声明一个或多个局部变量,作为资源用于 try 语句

      因此您不再需要关闭资源。 Try-with-resources 会自动为您执行此操作,您的 FileReader 将仅在 try 块中可用。因此你会得到那个编译错误。

      【讨论】:

      • 这是否意味着不需要finally 块或不需要just closing files
      • @user2708477 这意味着关闭资源不需要finally 块。如果您需要做其他事情,即使发生Exception 也无法跳过,那么您仍然需要finally 块。但是,如果您只是想确保在任何情况下都将关闭文件,则不需要,因为这将隐式完成。
      【解决方案3】:

      由于没有其他人提到这一点,如果您想手动处理它,您可以执行以下操作:

      private void generateSecretWord(String filename){
              FileReader files = null;
              try {
                  files = new FileReader(filename);
                  Scanner input = new Scanner(files);
                  String line = input.nextLine();
                  String[] words = line.split(",");
                  Collections.shuffle(Arrays.asList(words));
                  if (words[0].length()>1){
                      secretWord = words[0];
                      return;
                  }
      
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              }
              finally {
                  if (files!=null) 
                      files.close(); 
              }
      
          }
      

      【讨论】:

        【解决方案4】:

        您尝试执行的代码是 Java 7 之前的老式代码,必须关闭资源以避免内存泄漏。但是在 New Java 7 中巧妙地使用了它,即使最终阻塞也无法访问,也不需要关闭资源。

        【讨论】:

          【解决方案5】:

          在 Pom.xml 中将 java 设置为 1.7 或更高版本 如下,

          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <maven.compiler.source>1.8</maven.compiler.source>
              <maven.compiler.target>1.8</maven.compiler.target>
              <java.version>1.8</java.version>
          </properties>
          

          【讨论】:

            猜你喜欢
            • 2013-01-05
            • 1970-01-01
            • 2013-02-21
            • 2013-03-19
            • 1970-01-01
            • 1970-01-01
            • 2019-04-05
            • 1970-01-01
            • 2013-10-24
            相关资源
            最近更新 更多