Java Utililty Methods Path Copy nio

List of utility methods to do Path Copy nio

Description

The list of methods to do Path Copy nio are organized into topic(s).

Method

voidaddCopyRight(Path p)
add Copy Right
StringBuilder sb = new StringBuilder();
sb.append(COPY_RIGHT_STRING);
Files.readAllLines(p).forEach(l -> {
    sb.append(l);
    sb.append("\n");
});
Files.write(p, sb.toString().getBytes());
voidcopy(FileInputStream from, FileOutputStream to)
Copies the file using NIO.
FileChannel source = null;
FileChannel destination = null;
try {
    source = from.getChannel();
    destination = to.getChannel();
    destination.transferFrom(source, 0, source.size());
} finally {
    smothlyClose(source);
...
voidcopy(final File source, final File target)
Copy.
if (source == null)
    throw new NullPointerException("source directory is null.");
if (target == null)
    throw new NullPointerException("target directory is null.");
if (source.isFile()) {
    File parentFolder = target.getParentFile();
    if (parentFolder != null) {
        parentFolder.mkdirs();
...
voidcopy(final Path source, final Path destination, final CopyOption... options)
copy
if (Files.notExists(destination)) {
    Files.createDirectories(destination);
Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        String fileRelativePath = relativizePath(source, file);
        Path destFile = destination.resolve(fileRelativePath);
...
Pathcopy(Path from, Path to)
Copy source file into target file if the target file exists, then the target file is replaced.
return Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);
voidcopy(Path inPath, Path outPath)
copy
try (BufferedReader in = Files.newBufferedReader(inPath, StandardCharsets.UTF_8);
        BufferedWriter out = Files.newBufferedWriter(outPath, StandardCharsets.UTF_8);) {
    String line = null;
    while ((line = in.readLine()) != null) {
        out.write(line);
    out.flush();
voidcopy(Path source, Path destination)
Safely copies files and directories with its content.
if (Files.isDirectory(source)) {
    if (Files.exists(destination)) {
        if (!Files.isDirectory(destination)) {
            throw new IllegalArgumentException(
                    String.format("fail to copy %s to %s " + "because former is not a directory",
                            source.toString(), destination.toString()));
    } else {
...
voidcopy(Path source, Path destination)
copy
if (source.equals(destination)) {
    return;
} else if (!Files.exists(source)) {
    throw new FileNotFoundException("No such file/dir to copy: " + source);
} else if (Files.isDirectory(source)) {
    if (!Files.exists(destination)) {
        Files.createDirectories(destination);
    try (DirectoryStream<Path> files = Files.newDirectoryStream(source)) {
        for (Path file : files) {
            copy(file, destination.resolve(file.getFileName()));
} else {
    Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
Files.setLastModifiedTime(destination, Files.getLastModifiedTime(source));
voidcopy(Path source, Path target)
copy
if (Files.isDirectory(source)) {
    if (Files.isDirectory(target)) {
        Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes basicFileAttributes)
                    throws IOException {
                Path relative = source.relativize(path);
                Path targetDir = target.resolve(relative);
...
voidcopy(Path source, Path target, CopyOption... options)
copy
Files.createDirectories(target.getParent());
for (Path currentSource : collect(source)) {
    Path currentTarget = target.resolve(source.relativize(currentSource));
    Files.copy(currentSource, currentTarget, options);