Java FileInputStream Copy copyFile(File fromFile, File toFile)

Here you can find the source of copyFile(File fromFile, File toFile)

Description

Copy a file

License

Apache License

Parameter

Parameter Description
fromFile a parameter
toFile a parameter

Declaration

public static boolean copyFile(File fromFile, File toFile) 

Method Source Code

//package com.java2s;
/**************************************************************
 * /*from w w  w  .  j av  a2 s.c  o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 *************************************************************/

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

import java.io.FileOutputStream;

import java.io.IOException;

public class Main {
    /**
     * Copy a file
     * 
     * @param fromFile
     * @param toFile
     * @return
     */
    public static boolean copyFile(File fromFile, File toFile) {
        if (toFile.isDirectory())
            toFile = new File(toFile, fromFile.getName());

        FileInputStream from = null;
        FileOutputStream to = null;
        try {
            from = new FileInputStream(fromFile);
            File p = toFile.getParentFile();
            if (p != null && !p.exists())
                p.mkdirs();
            to = new FileOutputStream(toFile);
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = from.read(buffer)) != -1)
                to.write(buffer, 0, bytesRead);

            return true;
        } catch (IOException e) {
            // Can't copy
            e.printStackTrace();
            return false;
        } finally {
            if (from != null)
                try {
                    from.close();
                } catch (IOException e) {
                }
            if (to != null)
                try {
                    to.close();
                } catch (IOException e) {
                }
        }
    }

    /**
     * Copy a file
     * 
     * @param fromFileName
     * @param toFileName
     * @return
     */
    public static boolean copyFile(String fromFileName, String toFileName) {
        return copyFile(new File(fromFileName), new File(toFileName));
    }
}

Related

  1. copyFile(File from, File to)
  2. copyFile(File from, File to, byte[] buf)
  3. copyFile(File from, File toDir)
  4. copyFile(File from, File toFile)
  5. copyFile(File fromFile, File toFile)
  6. copyFile(File fromFile, File toFile)
  7. copyFile(File fromFile, File toFile, IProgressMonitor monitor)
  8. copyFile(File in, File out)
  9. copyFile(File in, File out)