Java FileChannel Copy copyToTemp(File srcFile)

Here you can find the source of copyToTemp(File srcFile)

Description

copy To Temp

License

Open Source License

Declaration

public static File copyToTemp(File srcFile) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 The University of York.
 * 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
 * /*from   w  ww  .  j a v a  2  s .c om*/
 * Contributors:
 *     Dimitrios Kolovos - initial API and implementation
 ******************************************************************************/

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

import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    public static File copyToTemp(File srcFile) throws IOException {
        File tmpFile = File.createTempFile("filecompare", "tmp");
        if (srcFile.isDirectory()) {
            tmpFile.delete();
            tmpFile.mkdir();
        }
        copy(srcFile, tmpFile);
        return tmpFile;
    }

    public static void copy(File srcFile, File dstFile) throws IOException {
        if (srcFile.isDirectory()) {
            dstFile.mkdir();
            for (File entry : srcFile.listFiles()) {
                copy(entry, new File(dstFile, entry.getName()));
            }
        } else {
            // Based on the second answer in http://stackoverflow.com/questions/106770.
            FileInputStream isSrc = null;
            FileOutputStream osDst = null;
            FileChannel chSrc = null;
            FileChannel chDst = null;
            try {
                isSrc = new FileInputStream(srcFile);
                osDst = new FileOutputStream(dstFile);
                chSrc = isSrc.getChannel();
                chDst = osDst.getChannel();
                final long srcBytes = srcFile.length();
                long transferred = 0;
                while (transferred < srcBytes) {
                    transferred += chDst.transferFrom(chSrc, transferred, srcBytes);
                    chDst.position(transferred);
                }
            } finally {
                if (chDst != null) {
                    chDst.close();
                } else if (osDst != null) {
                    osDst.close();
                }

                if (chSrc != null) {
                    chSrc.close();
                } else if (isSrc != null) {
                    isSrc.close();
                }
            }
        }
    }
}

Related

  1. copyStream(InputStream in, long length, File out)
  2. copyStreamToFile(InputStream inStream, File file)
  3. copyThrowsOnException(final File source, final File destination)
  4. copyTo(File srcFile, File destFile)
  5. copyToFolderAs(File fromFile, File folder, String asName)
  6. copyToTempDirectory(final String file)
  7. copyTransfer(String srcPath, String destPath)
  8. copyTree(File sourceLocation, File targetLocation)
  9. copyTree(File srcFile, File trgFile)