import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Stream; public class ListFilesExample { public static void main(String[] args) { String dirPath = "/path/to/directory"; try (Streampaths = Files.list(Paths.get(dirPath))) { paths.forEach(System.out::println); } catch (Exception e) { e.printStackTrace(); } } }
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FileExistsExample { public static void main(String[] args) { String filePath = "/path/to/file.txt"; Path path = Paths.get(filePath); boolean exists = Files.exists(path); System.out.println("File exists: " + exists); } }This code uses the Files.exists() method to check if a file exists at the specified path. It then prints whether the file exists or not. The package library for the java.nio.file package is "java.nio".