Here you can find the source of copyFile(String source, String target)
Parameter | Description |
---|---|
source | source file to copy. |
target | destination to copy to. |
Parameter | Description |
---|---|
IOException | when unable to copy. |
public static File copyFile(String source, String target) throws IOException
//package com.java2s; //License from project: Apache License import java.io.*; import java.nio.channels.FileChannel; public class Main { /**/* ww w. ja va 2s . c om*/ * Copies source file to target. * @param source source file to copy. * @param target destination to copy to. * @return target as File. * @throws IOException when unable to copy. */ public static File copyFile(String source, String target) throws IOException { FileChannel inputChannel = null; FileChannel outputChannel = null; try { inputChannel = new FileInputStream(source).getChannel(); outputChannel = new FileOutputStream(target).getChannel(); outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); } finally { if (inputChannel != null) { inputChannel.close(); } if (outputChannel != null) { outputChannel.close(); } } return new File(target); } }