@Override public ByteSink createSink() throws IOException { Path file = createFile(); if (initialBytes != null) { java.nio.file.Files.write(file, initialBytes); return MoreFiles.asByteSink(file, StandardOpenOption.APPEND); } return MoreFiles.asByteSink(file); }
@Override public CharSink createSink() throws IOException { Path file = createFile(); if (initialString != null) { try (Writer writer = java.nio.file.Files.newBufferedWriter(file, Charsets.UTF_8)) { writer.write(initialString); } return MoreFiles.asCharSink(file, Charsets.UTF_8, StandardOpenOption.APPEND); } return MoreFiles.asCharSink(file, Charsets.UTF_8); }
public void copy(Path source, Path target, CopySourceMode sourceMode) throws IOException { switch (sourceMode) { case FILE: Files.copy(resolve(source), resolve(target), StandardCopyOption.REPLACE_EXISTING); break; case DIRECTORY_CONTENTS_ONLY: MoreFiles.copyRecursively(resolve(source), resolve(target)); break; case DIRECTORY_AND_CONTENTS: MoreFiles.copyRecursively(resolve(source), resolve(target.resolve(source.getFileName()))); break; } }
@Override public ByteSource createSource(byte[] bytes) throws IOException { checkNotNull(bytes); Path file = createFile(); java.nio.file.Files.write(file, bytes); return MoreFiles.asByteSource(file); }
@Override public CharSource createSource(String string) throws IOException { checkNotNull(string); Path file = createFile(); try (Writer writer = java.nio.file.Files.newBufferedWriter(file, Charsets.UTF_8)) { writer.write(string); } return MoreFiles.asCharSource(file, Charsets.UTF_8); }
public void createSymLink(Path symLink, Path realFile, boolean force) throws IOException { symLink = resolve(symLink); if (force) { Files.deleteIfExists(symLink); } if (Platform.detect() == Platform.WINDOWS) { if (isDirectory(realFile)) { // Creating symlinks to directories on Windows requires escalated privileges. We're just // going to have to copy things recursively. MoreFiles.copyRecursively(realFile, symLink); } else { // When sourcePath is relative, resolve it from the targetPath. We're creating a hard link // anyway. realFile = symLink.getParent().resolve(realFile).normalize(); Files.createLink(symLink, realFile); } } else { Files.createSymbolicLink(symLink, realFile); } }
/** * Recursively delete everything under the specified path. Ignore the failure if the file at the * specified path does not exist. */ public void deleteRecursivelyIfExists(Path pathRelativeToProjectRoot) throws IOException { MoreFiles.deleteRecursivelyIfExists(resolve(pathRelativeToProjectRoot)); }