Java FileInputStream Copy copyFile(File inputFile, File outputFile)

Here you can find the source of copyFile(File inputFile, File outputFile)

Description

Copies the contents of any disk file to a specified output file.

License

Open Source License

Parameter

Parameter Description
inputFile a File object
outputFile a File object

Exception

Parameter Description

Declaration

public static void copyFile(File inputFile, File outputFile) throws java.io.IOException 

Method Source Code

//package com.java2s;
/*//from  w w  w  .  j av  a  2s. c o  m
 *  Util in org.jpws.front.util
 *  file: Util.java
 * 
 *  Project Jpws-Front
 *  @author Wolfgang Keller
 *  Created 28.09.2004
 *  Version
 * 
 *  Copyright (c) 2005 by Wolfgang Keller, Munich, Germany
 * 
 This program is not freeware software but copyright protected to the author(s)
 stated above. However, you can use, redistribute and/or modify it under the terms 
 of the GNU General Public License as published by the Free Software Foundation, 
 version 2 of the License.
    
 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, or go to
 http://www.gnu.org/copyleft/gpl.html.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StreamCorruptedException;

import java.util.zip.CRC32;

public class Main {
    /** Copies the contents of any disk file to a specified output file.  If the
     *  output file is a relative path, it is made absolute against the directory
     *  of the input file.  The output file will be overwritten if it exist.
     *  A CRC32 check is performed to compare source and copy after the copy process
     *  and if results negative a <code>StreamCorruptedException</code> is thrown.
     *  Function reports errors to <code>System.err</code>.
     *  @param inputFile a File object
     *  @param outputFile a File object
     *  @throws java.io.IOException if the function could not be completed
     *  because of an IO or CRC check error
     */
    public static void copyFile(File inputFile, File outputFile) throws java.io.IOException {
        File parent;
        FileInputStream in = null;
        FileOutputStream out = null;
        CRC32 crcSum;
        int writeCrc;
        //      long time;

        // control parameter
        if (inputFile == null | outputFile == null)
            throw new IllegalArgumentException("null pointer");
        if (inputFile.equals(outputFile))
            throw new IllegalArgumentException("illegal self reference");

        byte[] buffer = new byte[2048];
        int len;

        try {
            // make output file absolute (if not already)
            parent = inputFile.getAbsoluteFile().getParentFile();
            if (!outputFile.isAbsolute())
                outputFile = new File(parent, outputFile.getPath());

            // make sure the directory for the output file exists
            ensureFilePath(outputFile, parent);

            // create file streams
            in = new FileInputStream(inputFile);
            out = new FileOutputStream(outputFile);
            //         time = inputFile.lastModified();
            crcSum = new CRC32();

            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
                crcSum.update(buffer, 0, len);
            }

            in.close();
            out.close();
            writeCrc = (int) crcSum.getValue();
            //         outputFile.setLastModified( time );

            // control output CRC
            in = new FileInputStream(outputFile);
            crcSum.reset();
            while ((len = in.read(buffer)) != -1)
                crcSum.update(buffer, 0, len);
            if (writeCrc != (int) crcSum.getValue())
                throw new StreamCorruptedException("bad copy CRC");
        }

        catch (IOException e) {
            System.err.println("*** error during file copy: " + outputFile.getAbsolutePath());
            System.err.println(e);
            throw e;
        } finally {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        }
    }

    /** Ensures the existence of the directory which may be part of the path
     *  specification of parameter <code>file</code>. If the specified  file is a
     *  relative path, it is made absolute against <code>defaultDir</code>. If
     *  <code>defaultDir</code> is <b>null</b> the System property "user.dir" is
     *  assumed as default directory.
     *  @return <b>true</b> if and only if the parent directory of the specified file
     *  exists after this function terminates
     */
    public static boolean ensureFilePath(File file, File defaultDir) {
        File parent;

        if ((parent = file.getParentFile()) != null)
            return ensureDirectory(parent, defaultDir);
        return true;
    }

    /** Ensures the existence of the directory specified by the parameter. If the
     *  directory does not exist, an attempt is performed to create it including all
     *  necessary parent directories that may be implied by the specification.
     *  @param dir File specifying the intended directory; if the specified
     *  path is a relative path, it is made absolute against <code>defaultDir</code>.
    *   If <code>defaultDir</code> is <b>null</b> the System directory "user.dir" is
     *  assumed as default.
     *  @return <b>true</b> if and only if the specified file exists and
     *  is a directory after this function terminates
     */
    public static boolean ensureDirectory(File dir, File defaultDir) {
        boolean success = true;

        if (dir == null)
            throw new IllegalArgumentException("dir = null");

        if (!dir.isAbsolute())
            dir = new File(defaultDir, dir.getPath());

        if (!dir.isDirectory()) {
            success = !dir.isFile() && dir.mkdirs();
            if (!success)
                System.err.println("failed while trying to create directory: " + dir.toString());
        }

        return success;
    }
}

Related

  1. copyFile(File inFile, File outFile)
  2. copyFile(File inFile, File outFile)
  3. copyFile(File input, File output)
  4. copyFile(File inputFile, File outputFile)
  5. copyFile(File inputFile, File outputFile)
  6. copyFile(File inputFile, OutputStream os)
  7. copyFile(File of, File nf)
  8. copyFile(File oldFile, File newFile)
  9. copyFile(File oldFile, String newPath)