Introduction
In this project, you will learn how to search for text files in a directory using Java. This project will guide you through the process of getting the input path from the user, validating the input, finding all the text files in the directory, and printing their names.
👀 Preview
Please enter the read path:/home/labex/project/files
Found text files:
file1.txt
file3.txt
file6.txt
🎯 Tasks
In this project, you will learn:
- How to use the
Scannerclass to get user input for the directory path - How to validate the input path using the
Fileclass - How to create a custom
FilenameFilterto filter text files - How to use the
listFiles()method to find all the text files in the directory - How to iterate through the found text files and print their names
🏆 Achievements
After completing this project, you will be able to:
- Obtain user input for a directory path using the
Scannerclass - Validate the input path using the
Fileclass - Implement a custom
FilenameFilterto filter text files - Utilize the
listFiles()method to retrieve all text files in a directory - Iterate through the found text files and print their names
Get the Input Path
In this step, you will learn how to use the Scanner class to get the specified path from the user.
- Open the
SelectTxtFile.javafile in your code editor. - In the
mainmethod, create a newScannerobject to read user input:
Scanner scanner = new Scanner(System.in);
- Display a prompt message to the user asking for the read path:
System.out.print("Please enter the read path:");
- Use the
nextLine()method to read the user's input and store it in thepathvariable:
String path = scanner.nextLine();
- After reading the input, close the
Scannerto release the resources:
scanner.close();
Validate the Input Path
In this step, you will learn how to use the File class to check if the input path is a valid directory.
- Create a new
Fileobject with the user-providedpath:
File directory = new File(path);
- Use the
exists()method to check if the directory exists:
if (!directory.exists() || !directory.isDirectory()) {
- If the directory does not exist or is not a directory, print an error message and exit the program:
System.out.println("The directory does not exist, or it is not a directory");
return;
}
Find Text Files in the Directory
In this step, you will learn how to use a FilenameFilter to find all the text files in the directory.
- Create a new
TxtFileFilterclass that implements theFilenameFilterinterface:
static class TxtFileFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".txt");
}
}
- Use the
listFiles()method of theFileclass to get an array of files in the directory, filtering them with theTxtFileFilter:
File[] files = directory.listFiles(new TxtFileFilter());
Print the Found Text Files
In this final step, you will learn how to iterate through the found text files and print their names.
- Print a header to indicate that text files have been found:
System.out.println("Found text files:");
- Use a
for-eachloop to iterate through thefilesarray and print the name of each file:
for (File file : files) {
System.out.println(file.getName());
}
That's it! You have now completed the project of searching for text files in a directory. You can test your program by running the SelectTxtFile class.
Verifying Java Program for File Search
In this step, we will verify the file search Java program.
- Open your terminal and compile the Java file using the
javaccommand:
javac SelectTxtFile.java
- After successfully compiling the Java file, execute the compiled program using the
javacommand:
java SelectTxtFile
- Upon running the Java program, you'll be prompted to enter the read path. Provide the directory path where the text files are located and press Enter.
- If the directory does not exist or is not a directory, you'll receive a message indicating the issue.
- If the directory is valid and contains text files, the program will display the names of the text files found in that directory.
Here's an example of expected output:
Please enter the read path:/home/labex/project/files
Found text files:
file1.txt
file3.txt
file6.txt
By following these steps, you can verify the functionality of the Java program for selecting text files.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



