Java BufferedInputStream Copy copyFile(File src, File targetDir, boolean onlyNew)

Here you can find the source of copyFile(File src, File targetDir, boolean onlyNew)

Description

copy File

License

Open Source License

Declaration

private static void copyFile(File src, File targetDir, boolean onlyNew) throws IOException 

Method Source Code

//package com.java2s;
/*//from   w  w  w.ja  v a  2 s .  c  o m
 * TV-Browser
 * Copyright (C) 04-2003 Martin Oberhauser (martin_oat@yahoo.de)
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * CVS information:
 *  $RCSfile$
 *   $Source$
 *     $Date: 2010-11-21 15:38:33 +0100 (Sun, 21 Nov 2010) $
 *   $Author: bananeweizen $
 * $Revision: 6835 $
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    private static void copyFile(File src, File targetDir, boolean onlyNew) throws IOException {
        File destFile = new File(targetDir, src.getName());
        copy(src, destFile, onlyNew);
    }

    /**
     * Copies files given in source to the target directory.
     *
     * @param src The files to copy.
     * @param targetDir The target dir of the files.
     * @throws IOException Thrown if something goes wrong.
     */
    public static void copy(File[] src, File targetDir) throws IOException {
        copy(src, targetDir, false);
    }

    /**
     * Copies files given in source to the target directory.
     *
     * @param src The files to copy.
     * @param targetDir The target dir of the files.
     * @param onlyNew Overwrite only older files.
     * @throws IOException Thrown if something goes wrong.
     * @since 2.2.2/2.5.1
     */
    public static void copy(File[] src, File targetDir, boolean onlyNew) throws IOException {
        copy(targetDir, src, targetDir, onlyNew);
    }

    private static void copy(File firstTargetDir, File[] src, File targetDir, boolean onlyNew) throws IOException {
        // src might be null, if listFiles wasn't able to read the directory
        if (src == null) {
            return;
        }
        for (int i = 0; i < src.length; i++) {
            if (src[i].isDirectory() && !src[i].equals(targetDir) && !src[i].equals(firstTargetDir)) {
                File newDir = createDirectory(targetDir, src[i].getName());
                copy(firstTargetDir, src[i].listFiles(), newDir, onlyNew);
            } else {
                copyFile(src[i], targetDir, onlyNew);
            }
        }
    }

    /**
     * Copies a file.
     *
     * @param src The file to read from
     * @param target The file to write to
     * @throws IOException If copying failed
     */
    public static void copy(File src, File target) throws IOException {
        copy(src, target, false);
    }

    /**
     * Copies a file.
     *
     * @param src The file to read from
     * @param target The file to write to
     * @param onlyNew Overwrite only older files.
     * @throws IOException If copying failed
     * @since 2.2.2/2.5.1
     */
    public static void copy(File src, File target, boolean onlyNew) throws IOException {
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            FileOutputStream outFile = new FileOutputStream(target);
            in = new BufferedInputStream(new FileInputStream(src), 0x4000);
            out = new BufferedOutputStream(outFile, 0x4000);

            if (!onlyNew || target.length() < 1 || (src.lastModified() > target.lastModified())) {
                outFile.getChannel().truncate(0);
                pipeStreams(in, out);
            }

            in.close();
            out.close();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException exc) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException exc) {
                }
            }
        }
    }

    private static File createDirectory(File targetDir, String dirName) throws IOException {
        File f = new File(targetDir.getAbsolutePath() + "/" + dirName);
        if (!f.exists()) {
            if (!f.mkdirs()) {
                throw new IOException("Could not create directory '" + f.getAbsolutePath() + "'");
            }
        }
        return f;
    }

    /**
     * Pipes all data from the specified InputStream to the specified OutputStream,
     * until the InputStream has no more data.
     * <p>
     * Note: None of the streams is closed! You have to do that for yourself!
     *
     * @param from The stream to read the data from.
     * @param to The stream to write the data to.
     * @throws IOException Thrown if something goes wrong.
     */
    public static void pipeStreams(InputStream from, OutputStream to) throws IOException {
        int len;
        byte[] buffer = new byte[10240];
        while ((len = (from.read(buffer))) != -1) {
            to.write(buffer, 0, len);
        }
    }
}

Related

  1. copyFile(File src, File dest)
  2. copyFile(File src, File dst)
  3. copyFile(File src, File dst)
  4. copyFile(File src, File dst)
  5. copyFile(File src, File target)
  6. copyFile(File src, String target)
  7. copyFile(File srcFile, File destFile, boolean createCopy)
  8. copyFile(File srcFile, File detFolder)
  9. copyFile(File srcFile, File targetFolder)