How to search for a file in a directory in java

You can use the File class and the FilenameFilter interface to search for files in Java. The FilenameFilter interface is used to filter files from the file list. This interface has a boolean accept(File dir, String name) method, which is used to find the required file from the list returned by the java.io.File.list() method. This method is very useful when we want to find files with a specific extension in a folder.

First Approach

  1. Create a category MyFilenameFilter which implements the FilenameFilter interface and overrides the accept() method of FilenameFilter interface.
  2. The accept() method takes two arguments of which the primary one is that the directory name and therefore the other is that the filename.
  3. The accept() method returns true if the filename starts with the required initials else returns false.
  4. The class FindFile contains the most method which accepts the user input just like the desired directory to look and therefore the initials of the file to look .
  5. The directory object of the File class is initiated with the director name and therefore the filter object of MyFilenameFilter class is initiated with the initials provided by the user.
  6. The list() method is invoked on the dir object which returns an array of files that satisfy the condition.
  7. The array is iterated over and therefore the name of the specified files are printed to the output screen.

Code Implementation

 
// Java Program to Search for a File in a Directory
import java.io.*;

// MyFilenameFilter class implements FilenameFilter
// interface
class MyFilenameFilter implements FilenameFilter {
	
	String initials;
	
	// constructor to initialize object
	public MyFilenameFilter(String initials)
	{
		this.initials = initials;
	}
	
	// overriding the accept method of FilenameFilter
	// interface
	public boolean accept(File dir, String name)
	{
		return name.startsWith(initials);
	}
}

public class Main {
	
	public static void main(String[] args)
	{
		// Create an object of the File class
		// Replace the file path with path of the directory
		File directory = new File("/home/user/");

		// Create an object of Class MyFilenameFilter
		// Constructor with name of file which is being
		// searched
		MyFilenameFilter filter
			= new MyFilenameFilter("file.cpp");

		// store all names with same name
		// with/without extension
		String[] flist = directory.list(filter);

		// Empty array
		if (flist == null) {
			System.out.println(
				"Empty directory or directory does not exists.");
		}
		else {

			// Print all files with same name in directory
			// as provided in object of MyFilenameFilter
			// class
			for (int i = 0; i < flist.length; i++) {
				System.out.println(flist[i]+" found");
			}
		}
	}
}

Output

file.cpp found

Second Approach

  1. Each enter the ‘flist’ array is checked against the specified filename.
  2. If a match is found it's printed on the screen.
This method is a bit different from the previous one as the user needs to specify the exact name of the file in this case. 

Code Implementation

 
// Java Program to Search for a File in a Directory
import java.io.File;

public class Main {
	
	public static void main(String[] argv) throws Exception
	{
		// Create an object of the File class
		// Replace the file path with path of the directory
		File directory = new File("/home/user/");

		// store all names with same name
		// with/without extension
		String[] flist = directory.list();
		int flag = 0;
		if (flist == null) {
			System.out.println("Empty directory.");
		}
		else {

			// Linear search in the array
			for (int i = 0; i < flist.length; i++) {
				String filename = flist[i];
				if (filename.equalsIgnoreCase("file.cpp")) {
					System.out.println(filename + " found");
					flag = 1;
				}
			}
		}

		if (flag == 0) {
			System.out.println("File Not Found");
		}
	}
}

Output

file.cpp found

 

Submit Your Programming Assignment Details