How to check if Filepsth is Normal File or Directory in JAVA ?

In our previous post, we seen how to identify if a file is present or Not using JAVA

If you want to check whether certain filepath is a normal file or is a directory present in your disk before proceeding further to do some operations in JAVA, the class provides an API “isFile()” and “isDirectory()” using which we can detect if filepath is normal file or directory.

 $ vim 

public class FileIsDirectoryOrNormalFile {
        private static void checkIfFilepathIsDirectoryOrFile(String filePath) {
                File filePointer = new File(filePath);

                if (filePointer.exists() && filePointer.isFile()) {
                        System.out.println("Filepath: " + filePath + " is a file");
                } else if (filePointer.exists() && filePointer.isDirectory()) {
                        System.out.println("Filepath: " + filePath + " is a Directory");
                } else if (!filePointer.exists()){
                        System.out.println("Filepath: " + filePath + " not be present");

        public static void main(String[] args) throws Exception {
                checkIfFilepathIsDirectoryOrFile("/opt/xml"); //some random name for file, which may not be present

As, we can see in above program, we passed three different argument to our written function checkIfFilepathIsDirectoryOrFile, the first one is “/opt” which is a directory in Linux filesystem to demonstrate that our code detects its presence and shows that it is a Directory and second is “/bin/ls” which is a normal file in Linux filesystem, which we use as command to see files in current directory and third is “/opt/xml” which is a non-existent directory.

 $ javac 
 $ java FileIsDirectoryOrNormalFile 
Filepath: /opt/ is a Directory
Filepath: /bin/ls is a file
Filepath: /opt/xml not be present

The API definition is as below,

public boolean isFile () – Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory. Returns true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise

Related :   C program to monitor and notify changes in a directory / file using inotify

public boolean isDirectory () – Tests whether the file denoted by this abstract pathname is a directory. Returnstrue if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise

Android Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures Flutter git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized User Interface Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question