How to Check If File , Directory Exists Or Not Using Java

 Posted On  | Yashwant Chavan 

Its good programming practice to perform check, whether the file is exists or not before performing any file IO operation. Below Java code used to determine the file or folder is exists or not on your hard drive.

Check file - folder exists or not

fileOrFolder.exists() return the Boolean value , returns true if file or folder exists else returns false value.

package com.technicalkeeda.app;

import java.io.File;

public class FileDirectoryExists {

    public static void main(String[] args) {
        File fileOrFolder = new File("C:\\img");

        if (fileOrFolder.exists()) {
            System.out.println("File Folder Exist:- " + fileOrFolder.exists());
        } else {
            System.out.println("Sorry File Or Folder not found..");
        }
    }
}

output

File Folder Exist:- true


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0223 seconds.