0

This is my code to search for the text file with it's file name. there is no error in this program but i'm not knowing whether the code is searching properly or not. i want to get the message "file found" if the file exist if not "file not found" in the message dialog box. so any one who have an idea with my difficulties please help me.

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

Thanks a lot!!

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33

2 Answers2

0

File has an exists() method. To get a File from those parameters, create a new File. The code would like something like boolean fileExists = new File(dir, fileName).exists();

I guess that you're not looking for help on how to display test in a JLabel.

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
0

The answer to your question can be found here:

File f = new File(filePathString);
if(f.exists() && !f.isDirectory()) { /* do something */ }

You should not only use File.exists(), because it returns true also for directories. As you are interested in files only, you need the second check !File.isDirectory().

Community
  • 1
  • 1
Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33