Java FileInputStream Copy copyFile(File orig, File dest, boolean overwrite)

Here you can find the source of copyFile(File orig, File dest, boolean overwrite)

Description

copy File

License

Open Source License

Declaration

public static boolean copyFile(File orig, File dest, boolean overwrite) 

Method Source Code


//package com.java2s;
/*/*from w w  w. jav a2 s .  com*/
 * Utils.java - Created on Feb 2, 2004
 * 
 * Copyright (C) 2003-2004 Sebastian Rodriguez
 *
 * This library 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.1 of the License, or (at your option) any later version.
    
 * This library 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
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Last Update: $Date: 2004/03/12 17:25:14 $
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static boolean copyFile(File orig, File dest, boolean overwrite) {
        if (!orig.exists() && !orig.canRead())
            return false;
        if (dest.exists() && !overwrite)
            return false;

        try {
            //         FileReader in = new FileReader(orig);
            //         FileWriter out = new FileWriter(dest);
            //         int c;
            //         
            //         int count=0;
            //         while ((c = in.read()) != -1){
            //            out.write(c);
            //            count++;
            //         }
            //
            InputStream in = new FileInputStream(orig);
            FileOutputStream out = new FileOutputStream(dest);
            return copyStream(in, out);
        } catch (FileNotFoundException e) {
            //log.debug("FileNotFoundException caught",e);
            e.printStackTrace();
        } catch (IOException e) {
            //log.debug("IOException caught",e);
            e.printStackTrace();
        }
        return false;

    }

    public static boolean copyStream(InputStream in, FileOutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();
        return true;
    }
}

Related

  1. copyFile(File inputFile, OutputStream os)
  2. copyFile(File of, File nf)
  3. copyFile(File oldFile, File newFile)
  4. copyFile(File oldFile, String newPath)
  5. copyFile(File orig, File dest)
  6. copyFile(File original, File copy)
  7. copyFile(File original, File destination)
  8. copyFile(File original, File parent)
  9. copyFile(File originalFile, File destinationDir)