import java.io.File; public class FileExistsExample { public static void main(String[] args) { File file = new File("path/to/file.txt"); if (file.exists()) { System.out.println("File exists!"); } else { System.out.println("File does not exist."); } } }
import java.io.File; public class DirectoryExistsExample { public static void main(String[] args) { File directory = new File("path/to/directory"); if (directory.exists() && directory.isDirectory()) { System.out.println("Directory exists!"); } else { System.out.println("Directory does not exist."); } } }The java.util package is a part of the Java standard library, which includes a set of classes and interfaces for working with collections, time, date, and other utility functions. The File class is a part of the java.io package, which provides classes for working with input and output streams and files.