boolean exists = (new File("filename")).exists();if (exists) {// File or directory exists}else {// File or directory does not exist}
In this phrase, we use the exists() method on the File object to determine if the file or directory represented by the File object exists. The exists() method will return true if the file or directory exists, otherwise it will return false.