Java FileInputStream Copy copyFile(File sourceFile, File targetDir)

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

Description

Copy a file on the file system from a source to a destination

License

Open Source License

Parameter

Parameter Description
sourceFile reference to the source file
targetDir reference to the target folder of the copy. The copy will have the same name of the original

Declaration

public static void copyFile(File sourceFile, File targetDir) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2005 - 2014 TIBCO Software Inc. All rights reserved.
 * http://www.jaspersoft.com./*from  w  w w  .j  a  v a  2 s  .c  o  m*/
 * 
 * Unless you have purchased  a commercial license agreement from Jaspersoft,
 * the following license terms  apply:
 * 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/

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

import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**
     * Copy a file on the file system from a source to a destination
     * 
     * @param sourceFile reference to the source file
     * @param targetDir reference to the target folder of the copy. The copy will have
     * the same name of the original
     */
    public static void copyFile(File sourceFile, File targetDir) {
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }
        File copy = new File(targetDir.getAbsolutePath() + File.separatorChar + sourceFile.getName());
        InputStream is = null;
        if (!copy.exists()) {
            try {
                is = new FileInputStream(sourceFile);
                FileOutputStream fwr = new FileOutputStream(copy);
                copy.createNewFile();
                try {
                    byte[] buff = new byte[1024];
                    int read;
                    while ((read = is.read(buff)) != -1) {
                        fwr.write(buff, 0, read);
                    }
                } finally {
                    fwr.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (is != null)
                        is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Related

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