In this tutorial, we will see how to check if an expected folder (directory) exists or not using the JAVA code. In order to do so, first we would need to create a temporary folder first and then we will perform the existence check.

Simple example to test if Folder present or not in Java, Share It... Share on X

Let’s say:

If any temporary folder present at this path ‘C:\\Users\\isha’, then below code will through true else it will throw false. Lets examine that in the below code:

Let’s try to understand the code:

1. Creating the temp folder:

As mentioned at the top, first we need to create a temporary folder using below code.

2. Usage of ‘exists’ method:

Once the folder has been created, we are going to use ‘exists‘ and ‘isDirectory’ method of Java-IO library. They will test whether the folder is present or not and if it’s a directory or not, based on the output, it will throw ‘pass‘ or ‘fail‘ in return.

3. Additional check after deleting the folder:

Now, by using below line of code, we are deleting the created folder and performing the folder existence check again.

 

Lets run the above specified ‘VerifyFolderExists’ class.

Eclipse Console Output:  

 

That’s it, it’s that simple to check if File exists in Java :)

Simple example to test if Folder present or not in Java, Share It... Share on X

 

If you like this post , please check out my other useful blog posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    http://Techndeck.com deepak.rkgit@gmail.com Verma Deepak