Java Path Copy nio copyFile(Path source, Path target)

Here you can find the source of copyFile(Path source, Path target)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(Path source, Path target) throws IOException 

Method Source Code


//package com.java2s;
/*/*  w  w  w  .j  a v a  2  s. com*/
 * Copyright (c) 2015-2017, Excelsior LLC.
 *
 *  This file is part of Excelsior JET API.
 *
 *  Excelsior JET API 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.
 *
 *  Excelsior JET API 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 Excelsior JET API.
 *  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.io.*;
import java.nio.file.*;

public class Main {
    public static void copyFile(Path source, Path target) throws IOException {
        if (!target.toFile().exists()) {
            Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES);
        } else if (source.toFile().lastModified() != target.toFile().lastModified()) {
            //copy only files that were changed
            Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
        }

    }

    public static void copy(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024 * 1024];
        int len;
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
    }
}

Related

  1. copyFile(final String fileName, final String from, final String to)
  2. copyFile(InputStream from, Path to)
  3. copyFile(Path from, Path to)
  4. copyFile(Path source, Path destination)
  5. copyFile(Path source, Path destination, CopyOption... options)
  6. copyFile(Path source, Path target)
  7. copyFile(Path source, Path target, boolean foreign, CopyOption... options)
  8. copyFile(Path source, Path target, boolean okToOverwrite, boolean preserveAttributes)
  9. copyFile(Path source, Path target, boolean prompt, boolean preserve)