Determining if a File or Directory Exists

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.

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.