Java FileChannel Copy copyFile(File copyFrom, File copyTo)

Here you can find the source of copyFile(File copyFrom, File copyTo)

Description

copy File

License

Open Source License

Declaration

public static boolean copyFile(File copyFrom, File copyTo) throws IOException 

Method Source Code


//package com.java2s;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

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

import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class Main {

    public static boolean copyFile(File copyFrom, File copyTo) throws IOException {
        if (!isLockingSupported()) {
            return copyFileNormal(copyFrom, copyTo);
        } else {/*w w  w.ja  v a  2s.  co m*/
            return copyFileLocking(copyFrom, copyTo);
        }
    }

    public static boolean isLockingSupported() {
        boolean bool1 = System.getProperty("java.specification.version").compareTo("1.4") >= 0;
        boolean bool2 = (!(System.getProperty("os.name").toLowerCase().equals("hp-ux")))
                || (!(System.getProperty("os.arch").toLowerCase().startsWith("ia64")));
        return bool1 && bool2;
    }

    public static boolean copyFileNormal(File copyFrom, File copyTo) throws IOException {
        if (!copyFrom.exists() || copyTo == null) {
            return false;
        }

        byte[] arrayOfByte = new byte[4096];
        BufferedInputStream input = new BufferedInputStream(new FileInputStream(copyFrom));
        BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(copyTo, false));
        int i;
        while ((i = input.read(arrayOfByte, 0, arrayOfByte.length)) != -1) {
            output.write(arrayOfByte, 0, i);
        }
        output.close();
        input.close();
        return true;
    }

    public static boolean copyFileLocking(File copyFrom, File copyTo) throws FileNotFoundException, IOException {
        if ((!(copyFrom.exists())) || (copyTo == null)) {
            return false;
        }
        byte[] arrayOfByte = new byte[4096];
        FileOutputStream fileOutput = new FileOutputStream(copyTo, true);
        try {
            FileChannel channel = fileOutput.getChannel();
            FileLock fileLock = channel.tryLock();
            if (fileLock != null) {
                channel.truncate(0L);
                BufferedInputStream input = new BufferedInputStream(new FileInputStream(copyFrom));
                BufferedOutputStream output = new BufferedOutputStream(fileOutput);
                try {
                    int i;
                    while ((i = input.read(arrayOfByte, 0, arrayOfByte.length)) != -1) {
                        output.write(arrayOfByte, 0, i);
                    }
                    output.flush();
                    fileLock.release();
                } catch (Exception ex) {
                    throw new RuntimeException("copyFileLocking ERR : " + ex.getMessage(), ex);
                } finally {
                    output.close();
                    input.close();
                }
                return true;
            }
        } catch (Exception ex) {
            throw new RuntimeException("copyFileLocking ERR : " + ex.getMessage(), ex);
        } finally {
            fileOutput.close();
        }
        throw new IOException("Unable to acquire file lock for " + copyTo);
    }
}

Related

  1. copyAFile(File source, File target)
  2. copyAll(File source, File targetDir)
  3. copyAndReplace(File from, File to)
  4. copyChannel(ReadableByteChannel in, long length, FileChannel out)
  5. copyFile(File aFromFile, String aToFilename)
  6. copyFile(File file, String destDir)
  7. copyFile(File fileIn, File fileOut)
  8. copyFile(File from, File to)
  9. copyFile(File from, File to)