Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.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 如何在if语句条件下使用搜索到的文本文件?_Java_Swing_Search_Message - Fatal编程技术网

Java 如何在if语句条件下使用搜索到的文本文件?

Java 如何在if语句条件下使用搜索到的文本文件?,java,swing,search,message,Java,Swing,Search,Message,这是我用文件名搜索文本文件的代码。 这个程序没有错误,但我不知道代码是否正确搜索。如果文件存在,我想获取消息“文件已找到”,如果消息对话框中没有“文件未找到”。所以任何对我的困难有想法的人请帮助我 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: File dir = new File("C:\\Users\\Em

这是我用文件名搜索文本文件的代码。 这个程序没有错误,但我不知道代码是否正确搜索。如果文件存在,我想获取消息“文件已找到”,如果消息对话框中没有“文件未找到”。所以任何对我的困难有想法的人请帮助我

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

    File dir = new File("C:\\Users\\EmuD\\Documents\\NetBeansProjects\\Bank Project");
    File[] matchingFiles = dir.listFiles(new FilenameFilter() {

        public boolean accept(File dir, String name) {
            String search = jTextField1.getText();
            return name.startsWith(search) && name.endsWith(".txt");
        }
    });
}

非常感谢

文件
有一个
exists()
方法。要从这些参数中获取
文件
,请创建一个新文件。代码需要类似于
booleanfileexists=新文件(dir,fileName).exists()的内容


我猜您不是在寻求有关如何在JLabel中显示测试的帮助。

您的问题的答案可以在以下位置找到:


您不应该只使用
File.exists()
,因为它对目录也返回true。由于您只对文件感兴趣,因此需要再次检查
!File.isDirectory()

有什么困难?
File f = new File(filePathString);
if(f.exists() && !f.isDirectory()) { /* do something */ }