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

voidcopy(Path sourcePath, Path destinationPath)
Copy files.
String basedir = System.getProperty("basedir");
if (basedir == null) {
    basedir = Paths.get(".").toString();
sourcePath = Paths.get(basedir).resolve(sourcePath);
destinationPath = getCarbonHome().resolve(destinationPath);
createOutputFolderStructure(destinationPath);
try {
...
voidcopy(Path sourcePath, Path targetPath)
copy
if (!Files.exists(sourcePath)) {
    throw new IOException(sourcePath + " does not exist");
Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes basicFileAttributes)
            throws IOException {
        Files.createDirectories(targetPath.resolve(sourcePath.relativize(path)));
...
voidcopy(Path src, Path dest, List globPattern)
Copy the src file/dir to the dest path recursively.
dest.toFile().mkdirs();
FileSystem fs = FileSystems.getDefault();
List<PathMatcher> matchers = globPattern.stream().map(pattern -> fs.getPathMatcher("glob:" + pattern))
        .collect(Collectors.toList());
try (Stream<Path> stream = Files.walk(src)) {
    stream.filter(path -> {
        Path name = src.relativize(path);
        return matchers.stream().noneMatch(m -> m.matches(name));
...
Stringcopy(String filePath, String fileName)
Copies the given file to the temp directory.
String tempDirPath = System.getProperty("java.io.tmpdir");
Path destination = Paths.get(tempDirPath).resolve(fileName);
Path sourcePath = Paths.get(filePath);
Files.copy(sourcePath, destination, StandardCopyOption.REPLACE_EXISTING);
return tempDirPath;
voidcopyAlways(Path source, Path target)
copy Always
if (!Files.exists(target)) {
    Path parent = target.getParent();
    if (Files.notExists(parent)) {
        Files.createDirectories(parent);
    Files.createFile(target);
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
...
PathcopyAsTrueTempFile(Path source, FileAttribute... attrs)
copy As True Temp File
String suffix = null;
String fname = source.getFileName().toString();
if (fname.contains(".")) {
    String[] split = fname.split("\\.");
    suffix = "." + split[split.length - 1];
return copyAsTrueTempFile(source, null, suffix, attrs);
StringcopyConfigFile(final String file, final Path dir)
copy Config File
return copyConfigFile(Paths.get(file), dir, "testing-");
voidcopyContainer(String from, String to, IProgressMonitor monitor)
copy Container
Path fromPath = Paths.get(from);
Path toPath = Paths.get(to);
Path bakPath = null;
monitor.beginTask("Copy to target", 100);
if (Files.exists(toPath)) {
    bakPath = toPath.getParent().resolve(toPath.getFileName().toString() + ".bak");
    deleteDir(bakPath);
    Files.move(toPath, bakPath, StandardCopyOption.REPLACE_EXISTING);
...
voidcopyContents(Path from, Path to)
copy Contents
if (to == null || from == null)
    return;
if (Files.exists(to))
    Files.delete(to);
if (!from.toFile().renameTo(to.toFile()))
    throw new IOException("Could not move " + from.toFile().getPath() + " to " + to.toFile().getPath());
if (Files.exists(from))
    Files.delete(from);
...
voidcopyEverythingExcept(Path file, Path srcDir, Path dstDir, Predicate excluded, Consumer onCopy)
copy Everything Except
try {
    Files.list(file).forEach(f -> {
        if (!excluded.test(f)) {
            try {
                copyFile(f, srcDir, dstDir);
                if (Files.isRegularFile(f)) {
                    onCopy.accept(f);
                if (Files.isDirectory(f)) {
                    Files.createDirectories(f);
                    copyEverythingExcept(f, srcDir, dstDir, excluded, onCopy);
                } else if (!Files.isRegularFile(f)) {
                    throw new UnsupportedOperationException();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
    });
} catch (UncheckedIOException e) {
    throw e.getCause();