Java Path Copy nio copyResource(String source, Path target, Class cls)

Here you can find the source of copyResource(String source, Path target, Class cls)

Description

Copies class resource to path

License

Open Source License

Parameter

Parameter Description
source As in getResourceAsStream
target Target file
cls A class for finding class loader.

Exception

Parameter Description
IOException an exception

Declaration

public static final void copyResource(String source, Path target, Class<?> cls) throws IOException 

Method Source Code

//package com.java2s;
/*//ww w  . j  a  v  a 2  s . c o  m
 * Copyright (C) 2017 Timo Vesalainen <timo.vesalainen@iki.fi>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.io.InputStream;

import java.nio.file.CopyOption;

import java.nio.file.Files;
import java.nio.file.Path;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

import java.util.HashMap;
import java.util.Map;

public class Main {
    /**
     * Copies class resource to path
     * @param source As in getResourceAsStream
     * @param target Target file
     * @param cls A class for finding class loader.
     * @throws IOException 
     * @see java.lang.Class#getResourceAsStream(java.lang.String) 
     */
    public static final void copyResource(String source, Path target, Class<?> cls) throws IOException {
        try (InputStream is = cls.getResourceAsStream(source)) {
            Files.copy(is, target, REPLACE_EXISTING);
        }
    }

    /**
     * Enhances Files.copy to directory. If both source and target are 
     * directories the source directory structure is copied to target. Supports 
     * also copying from file system to another. I.e source and target doesn't 
     * have to be from same file system.
     * @param source
     * @param target
     * @param options
     * @throws IOException 
     * @see java.nio.file.Files#copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...) 
     */
    public static final void copy(Path source, Path target, CopyOption... options) throws IOException {
        if (Files.isRegularFile(target)) {
            if (Files.isRegularFile(target)) {
                Files.copy(source, target, options);
            } else {
                if (Files.isDirectory(target)) {
                    Path t = target.resolve(source.getFileName());
                    Files.copy(source, t, options);
                }
            }
        } else {
            if (Files.isDirectory(source) && Files.isDirectory(target)) {
                Map<Path, Path> fileMap = new HashMap<>();
                Path fileName = source.getFileName();
                final Path dir = fileName != null ? target.resolve(fileName) : target;
                Files.createDirectories(target);
                Files.walk(source).forEach((p) -> {
                    try {
                        Path trg = dir.resolve(source.relativize(p));
                        if (Files.isDirectory(p)) {
                            Files.copy(p, trg, options);
                        } else {
                            if (Files.isSymbolicLink(p)) {
                                Path symLink = Files.readSymbolicLink(p);
                                Path symTrg = source.relativize(symLink.toAbsolutePath());
                                Files.createSymbolicLink(trg, symTrg);
                            } else {
                                if (Files.isRegularFile(p)) {
                                    boolean hardLink = false;
                                    for (Path sp : fileMap.keySet()) {
                                        if (Files.isSameFile(p, sp)) {
                                            Path ht = fileMap.get(sp);
                                            Files.createLink(trg, ht);
                                            hardLink = true;
                                        }
                                    }
                                    if (!hardLink) {
                                        Files.copy(p, trg, options);
                                        fileMap.put(p, trg);
                                    }
                                }
                            }
                        }
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                });
            } else {
                throw new IllegalArgumentException("don't know what to do");
            }
        }
    }
}

Related

  1. copyRecursively(File src, File dest)
  2. copyRecursively(final Path source, final Path destination, final CopyOption... options)
  3. copyRecursively(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)
  4. copyRecursivelyHelper(Path source, Path target, AtomicLong bytesCounter, CopyOption... copyOptions)
  5. copyResource(String resourceLocation, Path target)
  6. copyResources(final URL source, final Path destination)
  7. copyToDir(Path source, Path targetDir, CopyOption... options)
  8. copyTree(final Path source, final Path target)
  9. createDirectories(Path currentRestorePath)