How to Check File is Directory or File in java With Examples

java.io.File class contains two methods using which we can find out if the file is a directory or a regular file in java.

  1. isFile(): This method returns true if file exists and is a regular file, note that if file doesn’t exist then it returns false.
  2. isDirectory(): This method returns true if file is actually a directory, if path doesn’t exist then it returns false.

While checking if a file is a directory or regular file, we should first check if the file exists or not. If it exists then only we should check if it’s a directory or file.

A simple java program showing it’s usage.


Output of the above program is:

By admin

Leave a Reply