Java File Copy doCopyFile(File srcFile, File destFile, boolean preserveFileDate)

Here you can find the source of doCopyFile(File srcFile, File destFile, boolean preserveFileDate)

Description

do Copy File

License

Apache License

Declaration

private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache 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 {
    private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
        if ((destFile.exists()) && (destFile.isDirectory())) {
            throw new IOException("Destination '" + destFile + "' exists but is a directory");
        }//from  w  w  w.j  ava2  s  .co m

        FileInputStream input = new FileInputStream(srcFile);
        try {
            FileOutputStream output = new FileOutputStream(destFile);
            try {
                copy(input, output);
            } finally {
                closeQuietly(output);
            }
        } finally {
            closeQuietly(input);
        }

        if (srcFile.length() != destFile.length()) {
            throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
        }

        if (preserveFileDate)
            destFile.setLastModified(srcFile.lastModified());
    }

    public static int copy(InputStream input, OutputStream output) throws IOException {
        long count = copyLarge(input, output);
        if (count > 2147483647L) {
            return -1;
        }
        return (int) count;
    }

    public static void closeQuietly(InputStream input) {
        try {
            if (input != null)
                input.close();
        } catch (IOException ioe) {
        }
    }

    public static void closeQuietly(OutputStream output) {
        try {
            if (output != null)
                output.close();
        } catch (IOException ioe) {
        }
    }

    public static long copyLarge(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[4096];
        long count = 0L;
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
            count += n;
        }
        return count;
    }
}

Related

  1. copyFiles(File srcFile, File destFolder)
  2. copyFiles(InputStream inStream, FileOutputStream outStream)
  3. copyFiles(String from, String to)
  4. copyFiles(String fromDirName, String toDirName)
  5. copyFileToPath(String fileToPath, String fileName, String sourceFile)
  6. doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
  7. fileCopy(File source, String dest)
  8. fileCopy(File srcFile, File tarFile)
  9. FileCopy(InputStream input, OutputStream output, int bufferSize)