Here you can find the source of doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
Parameter | Description |
---|---|
srcFile | the validated source file, must not be <code>null</code> |
destFile | the validated destination file, must not be <code>null</code> |
preserveFileDate | whether to preserve the file date |
Parameter | Description |
---|---|
IOException | if an error occurs |
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException
//package com.java2s; /*/*from ww w.ja v a 2 s . c o m*/ * $Id$ * -------------------------------------------------------------------------------------- * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com * * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.Channel; import java.nio.channels.FileChannel; public class Main { /** * Internal copy file method. * * @param srcFile the validated source file, must not be <code>null</code> * @param destFile the validated destination file, must not be <code>null</code> * @param preserveFileDate whether to preserve the file date * @throws IOException if an error occurs */ 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"); } FileChannel input = new FileInputStream(srcFile).getChannel(); try { FileChannel output = new FileOutputStream(destFile).getChannel(); try { output.transferFrom(input, 0, input.size()); } 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()); } } /** * Unconditionally close a <code>Channel</code>. * <p> * Equivalent to {@link Channel#close()}, except any exceptions will be ignored. * This is typically used in finally blocks. * * @param channel the Channel to close, may be null or already closed */ public static void closeQuietly(Channel channel) { try { if (channel != null) { channel.close(); } } catch (IOException ioe) { // ignore } } }