Java FileInputStream Copy copyFileFromTo(String filename, File fromDir, File toDir)

Here you can find the source of copyFileFromTo(String filename, File fromDir, File toDir)

Description

copy File From To

License

Open Source License

Declaration

private static void copyFileFromTo(String filename, File fromDir, File toDir) throws IOException 

Method Source Code


//package com.java2s;
/*/*ww  w.j a  v a 2 s  . co  m*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2011-2015 ForgeRock AS
 *      Portions Copyright 2013 Manuel Gaupp
 */

import java.io.*;

public class Main {
    private static void copyFileFromTo(String filename, File fromDir, File toDir) throws IOException {
        copyFile(new File(fromDir, filename), new File(toDir, filename));
    }

    /**
     * Copy a file.
     *
     * @param src
     *          The name of the source file.
     * @param dst
     *          The name of the destination file.
     * @throws IOException
     *           If the file could not be copied.
     */
    public static void copyFile(File src, File dst) throws IOException {
        copyOrAppend(src, dst, false);
    }

    private static void copyOrAppend(File src, File dst, boolean append) throws IOException {
        try (InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst, append)) {
            // Transfer bytes from in to out
            byte[] buf = new byte[8192];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
    }
}

Related

  1. copyFileBin(File sourceFile, File targetFile)
  2. copyFileBinary(String source, String destination)
  3. copyFileContents(File fromFile, File destination, boolean useTempFilenames)
  4. copyFileFolder(final File source, final File dest, final boolean override)
  5. copyFileFromFileToFile(File fromFile, File toFile)
  6. copyFileLowLevel(File source, File target)
  7. copyFileR(File srcFile, File destDirectory)
  8. copyFileRecursive(File src, File dest)
  9. copyFileToAnotherDirWithRelativePaths(File srcDir, File destDir, File originalFile)