Here you can find the source of copyFile(File source, File target)
Parameter | Description |
---|---|
source | - Source file. |
target | - Target file. |
Parameter | Description |
---|---|
InterruptedIOException | if thread is interrupted. Attempts to delete partial file if this occurs. |
IOException | if writing fails. |
public static void copyFile(File source, File target) throws IOException
/*/*from www . j a v a 2s . c o m*/ * Copyright (c) 2015. Philip DeCamp * Released under the BSD 2-Clause License * http://opensource.org/licenses/BSD-2-Clause */ import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main{ private static final String COPY_COMMAND; /** * Copies source file to target file. * * @param source - Source file. * @param target - Target file. * @throws InterruptedIOException if thread is interrupted. Attempts to delete partial file if this occurs. * @throws IOException if writing fails. */ public static void copyFile(File source, File target) throws IOException { Process p = Runtime.getRuntime().exec( new String[] { COPY_COMMAND, source.getAbsolutePath(), target.getAbsolutePath() }); try { int ret = p.waitFor(); if (ret != 0) throw new IOException("Failed to copy " + source.getAbsolutePath() + " to " + target.getAbsolutePath()); } catch (InterruptedException ex) { p.destroy(); target.delete(); throw new InterruptedIOException(ex.getMessage()); } } }