Path path = Paths.get("/home/user/data.txt"); // Create/delete Files.createDirectories(path.getParent()); Files.deleteIfExists(path); // Copy Files.copy(Paths.get("source.txt"), Paths.get("dest.txt"), StandardCopyOption.REPLACE_EXISTING); // Read all lines (small files) List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8); // Walk a directory try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("/home"))) for (Path entry : stream) System.out.println(entry.getFileName());
(file system events)
// br automatically closed try // some code catch (SQLException | IOException e) // single block logger.log(e); throw new MyAppException(e); java 7
WatchService watcher = FileSystems.getDefault().newWatchService(); Path dir = Paths.get("/tmp"); dir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE); WatchKey key = watcher.take(); for (WatchEvent<?> event : key.pollEvents()) System.out.println("Event: " + event.kind() + " on " + event.context()); Path path = Paths