Java FileInputStream Copy copyFile(File file, File dir)

Here you can find the source of copyFile(File file, File dir)

Description

Copy file file to location dir.

License

Apache License

Parameter

Parameter Description
file The file to copy.
dir The destination directory.

Exception

Parameter Description
IOException For various reason: if <code>file</code> does not exist or is not readable, if thedestination directory does not exist or isn't a directory, or if the file can't becopied for any reason.

Declaration

public static final void copyFile(File file, File dir) throws IOException 

Method Source Code

//package com.java2s;
/*//from  w  ww  .j  av  a 2 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 file <code>file</code> to location <code>dir</code>. This method will not fail silently.
     * Anything that prevents the copy from happening will be treated as an error condition. If the
     * method does not throw an exception, the copy was successful.
     * 
     * <p>
     * <b>Note: </b> this is a completely brain-dead implementation of file copy. The complete file is
     * read into memory before it is copied. If you need something more sophisticated for copying
     * large files, feel free to add your improvements.
     * 
     * @param file
     *          The file to copy.
     * @param dir
     *          The destination directory.
     * @throws IOException
     *           For various reason: if <code>file</code> does not exist or is not readable, if the
     *           destination directory does not exist or isn't a directory, or if the file can't be
     *           copied for any reason.
     */
    public static final void copyFile(File file, File dir) throws IOException {
        if (!file.exists() || !file.canRead()) {
            throw new IOException("File does not exist or is not readable: " + file.getAbsolutePath());
        }
        if (!dir.exists() || !dir.isDirectory()) {
            throw new IOException("Destination does not exist or is not a directory: " + dir.getAbsolutePath());
        }
        File outFile = new File(dir, file.getName());
        if (outFile.exists() && !outFile.canWrite()) {
            throw new IOException("Can't write output file: " + outFile);
        }
        byte[] bytes = new byte[(int) file.length()];
        FileInputStream is = null;
        FileOutputStream os = null;
        try {
            is = new FileInputStream(file);
            os = new FileOutputStream(outFile);

            while (true) {
                int count = is.read(bytes);
                if (0 > count) {
                    break;
                }
                os.write(bytes, 0, count);
            }
        } finally {
            if (null != is) {
                is.close();
            }
            if (null != os) {
                os.close();
            }
        }
    }
}

Related

  1. copyFile(File destfile, File srcfile)
  2. copyFile(File destFile, File srcFile)
  3. copyFile(File destination, File source)
  4. copyFile(File existingFile, File destFile)
  5. copyFile(File file, File destPath)
  6. copyFile(File file, File newFile, boolean overwrite, boolean setLastModified)
  7. copyFile(File file, OutputStream os)
  8. copyFile(File fileSrc, File fileDest)
  9. copyFile(File fileToCopy, File targetDir)