Java

Java File List Files in Ubuntu 22.04

“In this article, we will be focusing on the list files function in the File class in the Java programming language. The list files function returns the name of the files in the form of an array and is usually called in the File[] data type variable. The files class object is used for getting the directory name for the list files function so that it knows where to look for the files. We will be implementing several examples of this function in our article.”

Example # 01: Getting All the Names of the Files Present in a Directory Using the List Files Functions in Ubuntu 20.04

In this example, we will be creating a java program that will list all the names of files present in the given directory by using the list files function and the object of the File class.

In the above program, we have started with the try-and-catch approach for handling exceptions. The objective starts with the creation of an object of the File class in which the name of the directory is saved. Then we have created a File[] variable, which has the file class’s object calling the list files function. After this, the display message for the terminal is stated, and the for loop is initiated. In the for loop, the iterative flow of the name of files is displayed on the terminal with the help of the System.out.println function, which has the file[] variable along with the get name() function. After this, the catch block is stated for the exception and also ends our program for execution.

When we run the above program on our Ubuntu terminal, we will get the below output:

As we can see in the output above, the list of files is displayed on the terminal after the program is executed. We will verify the result of the program by opening the directory as shown below:

As we can see that the directory only has one file as the terminal output showed us, it verifies that our program showed us the correct list.

Example # 02: Getting All the Names of the Files Starting With the Same Alphabet Present in a Directory Using the List Files Functions in Ubuntu 20.04

We will write a Java program that will use the list files function and a File class object to list all the names of files in the specified directory that begin with the same alphabet in this example.

For handling exceptions, we began with the try-and-catch strategy in the preceding script. The objective begins with the development of a File class object in which the directory name is recorded. Following this, an object of the File name Filter class is generated. Following that, the Boolean statement is made, which includes the requirement of the name beginning with the letter “f.” Then we established a File[] variable that contains the file class’s object that calls the list files method. Following that, the display message for the terminal is specified, and the for loop is started. The repeated flow of file names is shown on the terminal in the for loop using the System.out.println function, which has the file[] variable as well as the get name() method. Following that, the catch block is mentioned for the exception, and our program is terminated for execution.

When we execute the following program in our Ubuntu terminal, we will obtain the result shown below:

After running the program, the terminal displays a list of file names beginning with the letter “f,” as seen in the output above. We will check the program’s output by accessing the directory given below:

As we can see from the terminal output, the directory contains two files with names beginning with the letter “f,” confirming that our program displayed the right list.

Example # 03: Getting All the Names of the Text Files in a Directory Using the List Files Functions in Ubuntu 20.04

In this example, we will write a Java program that will use the list files function and a File class object to list all the names of files in the specified directory that have the “.txt” extension.

In the prior script, we used the try-and-catch method to handle exceptions. The objective begins with the creation of a File class object that stores the directory name. Following that, a File name Filter class object is created. The Boolean statement is then executed, which contains the condition that the file is a text file. Then we created a File[] variable that holds the object of the file class that calls the list files function.

The display message for the terminal is then given, and the for loop is begun. In the for loop, the repeating flow of file names is shown on the terminal using the System.out.println function, which has the file[] variable as well as the get name() method. Following that, the catch block for the exception is specified, and our script is ended for execution.

When we run the following script in our Ubuntu terminal, we get the following result:

After executing the script, the terminal shows a list of files with the text file extension “.txt,” as shown in the output above. We’ll examine the program’s output by going to the directory listed below:

As shown in the output above, the directory has three files with the “.txt” extension, indicating that our script produced the correct list.

Conclusion

In this article, we have discussed the use of the List files function of the Files class in the Java programming language. The list files function is present in the Files class, where it returns an array of file names from the given directory and can even filter them with the user’s preference. We discussed and implemented several examples of the List files function in the Ubuntu 20.04 environment, where we explored the potential of this function while using multiple files in a directory and getting a list of names of files with similar extensions and even the same initial alphabet.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.