在Java try块中找不到符号

时间:2019-01-06 15:25:49

标签: java try-catch filenotfoundexception fileinputstream inputstreamreader

这是Java的新手,我正在尝试使用fileChooser打开文件并读取信息。我正在尝试创建fileInputStream和inputStreamReader的阶段。创建文件时,尽管文件已存在,但我仍被赋予FileNotFoundException。不太清楚为什么会发生这种情况,但是我已将此代码放入try/catch块中以解决它。不幸的是,在编译过程中,变量“ in”仍然出现cannot find symbol错误。如果有人可以解释这些问题,将不胜感激。

openFileBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {    
        JFileChooser fileChooser = new JFileChooser(); 
        fileChooser.setCurrentDirectory(new File("."));
        fileChooser.setDialogTitle("Open your Rain Data.");
        int returnVal = fileChooser.showOpenDialog(null);   
        //Handles when a file is opened by the user. 
        if (returnVal == JFileChooser.APPROVE_OPTION) {     
            String absolutePath = fileChooser.getSelectedFile().getAbsolutePath();
            File file = new File(absolutePath);
            try {
                FileInputStream in = new FileInputStream(file); 
                InputStreamReader reader = new InputStreamReader(in); 
            } catch (FileNotFoundException ex) {
                System.out.println("Error - the file was unable to be read by the rain handler. Check permissions and the file.");
            } finally {
                System.out.println(file.canRead()); 
                System.out.println(file.exists());
                System.out.println(in.available());     
            }   
        } 
    }
});

3 个答案:

答案 0 :(得分:1)

在特定范围内定义的变量,例如try主体仅在该范围内可见。因此,在try主体之外,该变量不可访问。
实际上,您应该在以下位置声明它:

File file = new File(absolutePath);
FileInputStream in = null;
try {
    in = new FileInputStream(file); 
    InputStreamReader reader = new InputStreamReader(in); 

} catch (FileNotFoundException ex) {
    System.out.println("Error - the file was unable to be read by the rain handler. Check permissions and the file.");

} finally {
    System.out.println(file.canRead()); 
    System.out.println(file.exists());
    System.out.println(in.available());     
}   

请注意,这是finally的错误用法。
finally子句确保:

  

finally块在try块和任何catch块之后执行   无论控件如何离开try块或   捕获块。

通常,您使用它来清除try正文中打开/使用的资源。
实际上,filein可能是null。因此,此代码可能会在运行时因NullPointerException而失败。
除了代码抛出一些IOException外,还应该捕获它们,而不仅仅是FileNotFoundException在错误处理中:

这种处理仅在try中执行,例如:

try {
    in = new FileInputStream(file); 
    InputStreamReader reader = new InputStreamReader(in); 
    System.out.println(file.canRead()); 
    System.out.println(file.exists());
    System.out.println(in.available());     

} 
 catch (FileNotFoundException ex) {
    System.out.println("Error - the file was unable to be read by the rain handler. Check permissions and the file.");
} 
 catch (IOException ex) {
        System.out.println("Error...");
 } 

还有一种更好的方法是依靠尝试使用资源来自动关闭流资源:

try (FileInputStream in = new FileInputStream(file);
      InputStreamReader reader = new InputStreamReader(in)){
     // ...
}

答案 1 :(得分:0)

标准方法是:

FileInputStream fileInputStream = null;
try {
    fileInputStream = new FileInputStream(...);
    // do something with the inputstream
} catch (IOException e) {
    // handle an exception
} finally { //  finally blocks are guaranteed to be executed
    // close() can throw an IOException too, so we got to wrap that too
    try {
        if (fileInputStream != null) {
            fileInputStream.close();
        }        
    } catch (IOException e) {
        // handle an exception, or often we just ignore it
    }
}

所以在您的情况下:

public void actionPerformed(ActionEvent e) {
     JFileChooser fileChooser = new JFileChooser();
     fileChooser.setCurrentDirectory(new File("."));
      fileChooser.setDialogTitle("Open your Rain Data.");
      int returnVal = fileChooser.showOpenDialog(null);
      //Handles when a file is opened by the user. 
      if (returnVal == JFileChooser.APPROVE_OPTION) {
          String absolutePath = fileChooser.getSelectedFile().getAbsolutePath();
          File file = new File(absolutePath);
          FileInputStream in = null;
          try {
              in = new FileInputStream(file);
              InputStreamReader reader = new InputStreamReader(in);
          } catch (FileNotFoundException ex) {
              System.out.println("Error - the file was unable to be read by the rain handler. Check permissions and the file.");
          } finally {
              System.out.println(file.canRead());
              System.out.println(file.exists());
              try {
                  System.out.println(in.available());
              } catch (IOException ex) {
                  // handle an exception, or often we just ignore it  
              }
          }
      }
  }

答案 2 :(得分:0)

FileInputStream in = new FileInputStream(file); 

在发布的问题中,在try块中创建了引用“ in”,因此它在该块中具有局部作用域。首选是在try块之前声明它,以具有更大的作用域。