Int ln=new File("F:\\13 reasons why").list().length You can also write the above code much more efficiently in the following way performing all the important operations in a single line of code.
![how to create a file folder in java how to create a file folder in java](https://www.cs.unc.edu/xcms/courses/comp110-ss1-11/images/exportJar1.png)
("The number of files in the directory is:"+ln) Here is the complete code for our program: import java.io.* įile file=new File("F:\\13 reasons why")
HOW TO CREATE A FILE FOLDER IN JAVA HOW TO
So now we know how to list all the items in a certain directory, but our task is to find the number of files in the directory.įor this purpose, we will use the length property of java which is used to find the length of arrays. Now when the above code will be executed all the files inside the directory will be listed in the output. import java.io.* įile file=new File("F:\\13 reasons why") //This is our directory which contains the video and subtitle files In the below code we have a directory which contains some video files and respective subtitle files. Now let’s see an example of the list() method and see how it works.
![how to create a file folder in java how to create a file folder in java](https://i0.wp.com/javaconceptoftheday.com/wp-content/uploads/2016/09/ListFilesInDirectory.png)
This method returns all the files in a certain directory in the form of a String array. The list() method in Java comes under the File class in Java. Today we’ll be learning to count the number of files in a certain directory using Java.īefore beginning, we need to get familiar few methods for the same.