How to Search a file in a Directory using Java

By | 2015-05-08T18:25:44+05:30 May 8th, 2015|java|

In the previous tutorial we have seen about count number of lines characters and words in a file Java. In this tutorial we are going to see How to Search a file in a Directory using Java. It a common scenario to search a file in our computer system. it is not only important for Java interview point of view but it is a common requirement to search a file. Here is the example to search a file in a directory.

Search a file in a Directory


import java.io.File;

/**
 *
 * @author chandrashekhar
 */
public class SearchFile {

    static final String DIR_PTH = "/home/chandrashekhar/Desktop";
    static final String FILE_NAME = "Onlinetutorialspoint.png";

    public static void main(String[] args) {
        try {
            SearchFile searchFile = new SearchFile();
            searchFile.seach_file(new File(DIR_PTH), FILE_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void seach_file(File file, String file_to_search) {
        try {
            if (file_to_search != null && !file_to_search.isEmpty()) {
                if (file != null) {
                    if (file.isDirectory()) {
                        //do you have permission to read this directory?    
                        if (file.canRead()) {
//                            System.out.println("Searching in : "+file.getAbsoluteFile());
                            for (File sub_directory : file.listFiles()) {
                                if (sub_directory.isDirectory()) {
                                    seach_file(sub_directory, file_to_search);
                                } else {
                                    if (file_to_search.equalsIgnoreCase(sub_directory.getName().toLowerCase())) {
                                        System.out.println("File Found @ : " + sub_directory.getAbsoluteFile().toString());
                                    }
                                }
                            }
                        } else {
                            System.out.println(file.getAbsoluteFile() + " Permission Denied");
                        }
                    } else {
                        System.out.println(file.getAbsoluteFile() + " is not a directory!");
                    }
                } else {
                    file = new File("/");
                    seach_file(file, file_to_search);
                }
            } else {
                System.out.println("The file given to search ");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output :

File Found @ : /home/chandrashekhar/Desktop/hello/Onlinetutorialspoint.png
File Found @ : /home/chandrashekhar/Desktop/Onlinetutorialspoint.png

You may also Like :

count number of lines characters and words in a file Java

Writing data to a file in Java

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment