import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.Path; Path path = Paths.get("path/to/file"); byte[] data = Files.readAllBytes(path);
@Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { Files.createDirectories(dest.resolve(src.relativize(dir))); return FileVisitResult.CONTINUE; } @Override
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.copy(file, dest.resolve(src.relativize(file)), StandardCopyOption.REPLACE_EXISTING); return FileVisitResult.CONTINUE; } });
/** * This implementation returns the underlying File reference. */ @Override public File getFile() { return (this.file != null ? this.file : this.filePath.toFile()); }
@CanIgnoreReturnValue private Path newDir(String name) throws IOException { Path dir = rootDir.resolve(name); Files.createDirectory(dir); return dir; }
/** * This implementation checks whether the underlying file is marked as readable * (and corresponds to an actual file with content, not to a directory). * @see java.nio.file.Files#isReadable(Path) * @see java.nio.file.Files#isDirectory(Path, java.nio.file.LinkOption...) */ @Override public boolean isReadable() { return (Files.isReadable(this.path) && !Files.isDirectory(this.path)); }
/** * This implementation checks whether the underlying file is marked as writable * (and corresponds to an actual file with content, not to a directory). * @see java.nio.file.Files#isWritable(Path) * @see java.nio.file.Files#isDirectory(Path, java.nio.file.LinkOption...) */ @Override public boolean isWritable() { return (Files.isWritable(this.path) && !Files.isDirectory(this.path)); }
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.deleteIfExists(file); return FileVisitResult.CONTINUE; }
/** * This implementation returns a URI for the underlying file. * @see java.nio.file.Path#toUri() */ @Override public URI getURI() throws IOException { return this.path.toUri(); }
/** * This implementation compares the underlying Path references. */ @Override public boolean equals(Object other) { return (this == other || (other instanceof PathResource && this.path.equals(((PathResource) other).path))); }
/** * This implementation returns the underlying file's length. */ @Override public long contentLength() throws IOException { return Files.size(this.path); }
@Override public void tearDown() throws IOException { rootDir.getFileSystem().close(); }
@SuppressWarnings("FilesLinesLeak") // the user needs to close it in this case @Override public Stream<String> lines() throws IOException { return Files.lines(path, charset); } };
private BasicFileAttributes readAttributes() throws IOException { return Files.readAttributes( path, BasicFileAttributes.class, followLinks ? FOLLOW_LINKS : new LinkOption[] {NOFOLLOW_LINKS}); }
/** * This implementation opens a Channel for the underlying file. * @see Files#newByteChannel(Path, OpenOption...) */ @Override public WritableByteChannel writableChannel() throws IOException { return Files.newByteChannel(this.path, StandardOpenOption.WRITE); }
@Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } });
@Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { java.nio.file.Files.deleteIfExists(file); return FileVisitResult.CONTINUE; }
@Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (exc != null) { return FileVisitResult.TERMINATE; } java.nio.file.Files.deleteIfExists(dir); return FileVisitResult.CONTINUE; } });
@Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { if (exc != null) { return FileVisitResult.TERMINATE; } Files.deleteIfExists(dir); return FileVisitResult.CONTINUE; } });