Java Path Copy nio copy(FileInputStream from, FileOutputStream to)

Here you can find the source of copy(FileInputStream from, FileOutputStream to)

Description

Copies the file using NIO.

License

Apache License

Parameter

Parameter Description
from File to copy
to Target file

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Declaration

public static void copy(FileInputStream from, FileOutputStream to) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    /**/*from w w  w . j  a  v a 2s.c  o m*/
     * Copy.
     *
     * @param source the source file or directory.
     * @param target the target file or directory.
     * @throws IOException when IO error occur.
     */
    public static void copy(final File source, final File target) throws IOException {
        if (source == null)
            throw new NullPointerException("source directory is null.");
        if (target == null)
            throw new NullPointerException("target directory is null.");

        if (source.isFile()) {
            Files.copy(source.toPath(), target.toPath(), StandardCopyOption.COPY_ATTRIBUTES,
                    StandardCopyOption.REPLACE_EXISTING);
        } else if (source.isDirectory()) {
            Files.walkFileTree(source.toPath(), new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs)
                        throws IOException {
                    Files.createDirectories(target.toPath().resolve(source.toPath().relativize(dir)));
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
                        throws IOException {
                    Files.copy(file, target.toPath().resolve(source.toPath().relativize(file)),
                            StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }

    /**
     * Copies the file using NIO.
     *
     * @param from          File to copy
     * @param to          Target file
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void copy(FileInputStream from, FileOutputStream to) throws IOException {

        FileChannel source = null;
        FileChannel destination = null;
        try {
            source = from.getChannel();
            destination = to.getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            smothlyClose(source);
            smothlyClose(destination);
        }
    }

    /**
     * Copy.
     *
     * @param from the from
     * @param to the to
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void copy(InputStream from, OutputStream to) throws IOException {
        try {
            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = from.read(bytes)) != -1) {
                to.write(bytes, 0, read);
            }
        } finally {
            smothlyClose(from);
            smothlyClose(to);
        }
    }

    /**
     * Creates the directories.
     *
     * @param path the path
     * @return true, if successful
     */
    public static boolean createDirectories(String path) {
        File directory = new File(path);
        return createDirectories(directory);
    }

    /**
     * Creates the directories.
     *
     * @param directory the directory
     * @return true, if successful
     */
    public static boolean createDirectories(File directory) {
        return directory.mkdirs();
    }

    /**
     * Smothly close.
     *
     * @param io the io
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void smothlyClose(Closeable io) throws IOException {
        if (io != null) {
            io.close();
        }
    }
}

Related

  1. addCopyRight(Path p)
  2. copy(final File source, final File target)
  3. copy(final Path source, final Path destination, final CopyOption... options)
  4. copy(Path from, Path to)
  5. copy(Path inPath, Path outPath)