【问题标题】:How to check if a file exists in javajava中如何判断文件是否存在
【发布时间】:2015-11-15 14:57:20
【问题描述】:

我在检查 Java 中是否存在文件时遇到问题。然而 IF 块似乎工作,但 ELSE 似乎没有。看,当文件存在时,它会提示一个框,上面写着“找到文件”。只要文件确实存在,就会在我的程序中发生这种情况,问题是当文件不存在时,我的控制台中会出现错误。有人可以告诉我对我的问题进行编码的更简单和更短的方法是什么?谢谢 !这是我的代码

        public void actionPerformed(ActionEvent e) {
             BufferedReader br = null;
             File f = new File(textField.getText());
             String path =  new String("C:\\Users\\theBeard\\workspace\\LeapYear\\");
            try {

                String sCurrentLine;

                br = new BufferedReader(new FileReader(path+f));
                if (f.exists())
                {
                    JOptionPane.showMessageDialog(null, textField.getText()+" found" );
                    while ((sCurrentLine = br.readLine()) != null) {

                         textArea.append(sCurrentLine);
                         textArea.append(System.lineSeparator());
                    }
                }
                else
                {
                    JOptionPane.showMessageDialog(null, textField.getText()+" not found" );
                }


            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (br != null)
                    {

                        br.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

    }


    });

【问题讨论】:

标签: java if-statement inputstream bufferedreader


【解决方案1】:

问题出在这一行:

br = new BufferedReader(new FileReader(path+f));
  1. 您将File 附加到String,这没有任何意义。您应该将String 附加到String,在这种情况下,textField.getText()) 附加到path。

  2. 根据FileReader的文档,如果文件不存在,此行将引发异常:

投掷: FileNotFoundException - 如果指定文件不存在,是目录而不是常规文件,或者由于某些其他原因无法打开读取。

这会导致您的程序到达catch 子句并打印异常堆栈跟踪。仅当f.exists() 返回 true 时才应调用此行:

if (f.exists())
{
    br = new BufferedReader(new FileReader(path + textField.getText()));
    ...
}

【讨论】:

    【解决方案2】:

    看看你的这些代码行:

    br = new BufferedReader(new FileReader(path+f));
    if (f.exists())
    

    您正在尝试打开文件之前检查它是否存在。因此,如果尝试打开它并返回 FileNotFoundException 失败,则永远无法进行测试。

    【讨论】:

      【解决方案3】:
      String path = "C:\\Path\\To\File\\Directory\\";
      String fileName = "NameOfFile.ext";
      File f = new File(path, fileName);
      if(f.exists()) {
          //<code for file existing>
      } else {
          //<code for file not existing>
      }
      

      【讨论】:

      • 请解释一下你的答案,只贴代码不构成完整答案
      【解决方案4】:

      您必须在检查文件是否存在后实例化 BufferedReader。

      String path =  new String("C:\\Users\\theBeard\\workspace\\LeapYear\\");
      File f = new File(path + textField.getText());
      ...
      if (f.exists())
      {
          br = new BufferedReader(new FileReader(f.getAbsolutePath()));  // or br = new BufferedReader(f);
          ...
      

      【讨论】:

        猜你喜欢
        • 2011-07-14
        • 1970-01-01
        • 1970-01-01
        • 2010-09-17
        • 1970-01-01
        • 2010-12-11
        • 2013-05-12
        • 1970-01-01
        • 2021-07-26
        相关资源
        最近更新 更多