How to write a java program to search for a file in a directory.

Looking at documents in java may be done using the report elegance and filenamefilter interface. The filenamefilter interface is used to clear out documents from the list of files. This interface has a way boolean receive(report dir, string call) this is carried out to locate the preferred documents from the list returned by way of the java. Io. File. Listing() method. This approach is very useful while we want to locate documents with a specific extension within a folder.

First Approach

  1. Create a class myfilenamefilter that implements the filenamefilter interface and overrides the be given() technique of filenamefilter interface.
  2. The accept() approach takes two arguments of which the primary one is the listing name and the second one is the filename.
  3. The receive() method returns genuine if the filename begins with the specified initials else returns fake.
  4. The class findfile carries the principle method which accepts the person input just like the favored listing to search and the initials of the file to look.
  5. The directory object of report magnificence is initiated with the director call and the clear out item of myfilenamefilter magnificence is initiated with the initials furnished via the user.
  6. The listing() method is invoked at the dir object  which returns an array of files that satisfy the condition.
  7. The array is iterated over and the call of the desired documents 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. The listing() technique is referred to as on the dir object of the report elegance and the listing of files within the ‘flist’ array.
  2. Each file in the ‘flist’ array is checked against the required filename.
  3. If a match is found it is printed on the screen.

This approach is a bit specific from the preceding one as the user wishes to specify the precise name of the document in this situation.

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