Java FileInputStream Copy copyFile(File sourceFile, File destinationFile)

Here you can find the source of copyFile(File sourceFile, File destinationFile)

Description

Copies the specified source file's content to the destination file's location.

License

Apache License

Parameter

Parameter Description
sourceFile the source file's location
destinationFile the destination file's location

Exception

Parameter Description
IOException thrown if the file cannot be copied

Declaration

public static void copyFile(File sourceFile, File destinationFile) throws IOException 

Method Source Code

//package com.java2s;
/**/*from w ww.  j a  va2 s .  com*/
 * Copyright (C) 2014 OpenTravel Alliance (info@opentravel.org)
 *
 * Licensed 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;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * Copies the specified source file's content to the destination file's location. If the
     * destination file already exists, it will be overwritten.
     * 
     * @param sourceFile
     *            the source file's location
     * @param destinationFile
     *            the destination file's location
     * @throws IOException
     *             thrown if the file cannot be copied
     */
    public static void copyFile(File sourceFile, File destinationFile) throws IOException {
        InputStream in = null;
        OutputStream out = null;
        try {
            if (!destinationFile.getParentFile().exists()) {
                destinationFile.getParentFile().mkdirs();
            }
            in = new FileInputStream(sourceFile);
            out = new FileOutputStream(destinationFile);
            byte[] buffer = new byte[1024];
            int bytesRead;

            while ((bytesRead = in.read(buffer)) >= 0) {
                out.write(buffer, 0, bytesRead);
            }

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

Related

  1. copyFile(File sourceFile, File destFile)
  2. copyFile(File sourceFile, File destFile)
  3. copyFile(File sourceFile, File destFile)
  4. copyFile(File sourceFile, File destFile)
  5. copyFile(File sourceFile, File destFile, boolean overwrite, boolean preserveLastModified)
  6. copyFile(File sourceFile, File destinationFile)
  7. copyFile(File sourceFile, File targetDir)
  8. copyFile(File sourceFile, File targetDir)
  9. copyFile(File sourceFile, File targetFile)