Java Path Copy nio copyAsTrueTempFile(Path source, FileAttribute... attrs)

Here you can find the source of copyAsTrueTempFile(Path source, FileAttribute... attrs)

Description

copy As True Temp File

License

Open Source License

Declaration

public static Path copyAsTrueTempFile(Path source, FileAttribute<?>... attrs) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.FileAttribute;

public class Main {
    public static Path copyAsTrueTempFile(Path source, FileAttribute<?>... attrs) throws IOException {
        String suffix = null;//  ww  w .j a v a2  s .  c o  m
        String fname = source.getFileName().toString();
        if (fname.contains(".")) {
            String[] split = fname.split("\\.");
            suffix = "." + split[split.length - 1];
        }
        return copyAsTrueTempFile(source, null, suffix, attrs);
    }

    public static Path copyAsTrueTempFile(Path source, String prefix, String suffix, FileAttribute<?>... attrs)
            throws IOException {
        Path tmp = createTrueTempFile(prefix, suffix, attrs);
        Files.copy(source, tmp, StandardCopyOption.REPLACE_EXISTING);
        return tmp;
    }

    public static Path createTrueTempFile(FileAttribute<?>... attrs) throws IOException {
        return createTrueTempFile(null, null, attrs);
    }

    public static Path createTrueTempFile(String prefix, String suffix, FileAttribute<?>... attrs)
            throws IOException {
        Path tmp = Files.createTempFile(prefix, suffix, attrs);
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    Files.delete(tmp);
                } catch (IOException e) {
                }
            }
        });
        return tmp;
    }
}

Related

  1. copy(Path sourcePath, Path destinationPath)
  2. copy(Path sourcePath, Path targetPath)
  3. copy(Path src, Path dest, List globPattern)
  4. copy(String filePath, String fileName)
  5. copyAlways(Path source, Path target)
  6. copyConfigFile(final String file, final Path dir)
  7. copyContainer(String from, String to, IProgressMonitor monitor)
  8. copyContents(Path from, Path to)
  9. copyEverythingExcept(Path file, Path srcDir, Path dstDir, Predicate excluded, Consumer onCopy)