Java FileInputStream Copy copyFileContents(File fromFile, File destination, boolean useTempFilenames)

Here you can find the source of copyFileContents(File fromFile, File destination, boolean useTempFilenames)

Description

Copy a file into destination destination .

License

Open Source License

Parameter

Parameter Description
fromFile a parameter
destination a parameter
useTempFilenames a parameter

Exception

Parameter Description
IOException an exception

Declaration

private static File copyFileContents(File fromFile, File destination, boolean useTempFilenames)
        throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2011 Red Hat Inc. and others.
 * All rights reserved. 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
 *
 * Contributors://from  ww  w. j a  v a2s  .c o m
 *     Red Hat Inc. - initial API and implementation
 *******************************************************************************/

import java.io.File;

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

import java.io.IOException;

public class Main {
    /**
     * Prefix for temporary directories created by EFP tests.
     */
    private static final String TMP_DIRECTORY_PREFIX = "eclipse-fedorapackager-tests-temp";

    /**
     * Copy a file into destination {@code destination}. If {@code useTempFile}
     * is set to true, the a random temporary file in destination will get
     * created. Otherwise the filename of {@code fromFile} will be used.
     * 
     * @param fromFile
     * @param destination
     * @param useTempFilenames
     * @return
     * @throws IOException
     */
    private static File copyFileContents(File fromFile, File destination, boolean useTempFilenames)
            throws IOException {
        FileInputStream from = null;
        FileOutputStream to = null;
        File toFile = null;
        if (useTempFilenames) {
            toFile = File.createTempFile(TMP_DIRECTORY_PREFIX, "", destination);
        } else {
            toFile = new File(destination.getAbsolutePath() + File.separatorChar + fromFile.getName());
        }
        try {
            from = new FileInputStream(fromFile);
            to = new FileOutputStream(toFile);
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = from.read(buffer)) != -1) {
                to.write(buffer, 0, bytesRead); // write
            }
        } finally {
            if (from != null) {
                try {
                    from.close();
                } catch (IOException e) {
                    // ignore
                }
            }
            if (to != null) {
                try {
                    to.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
        return toFile;
    }
}

Related

  1. copyFile(String target, String source)
  2. copyFileAndReName(String destPath, String srcPath, String srcFile, String destFile)
  3. copyFileAsStream(String srcFilePathName, String dstFilePathName)
  4. copyFileBin(File sourceFile, File targetFile)
  5. copyFileBinary(String source, String destination)
  6. copyFileFolder(final File source, final File dest, final boolean override)
  7. copyFileFromFileToFile(File fromFile, File toFile)
  8. copyFileFromTo(String filename, File fromDir, File toDir)
  9. copyFileLowLevel(File source, File target)