Here you can find the source of copyFile(final File source, final File target)
Parameter | Description |
---|---|
source | File |
target | File |
Parameter | Description |
---|---|
FileNotFoundException | an exception |
IOException | an exception |
public static void copyFile(final File source, final File target) throws FileNotFoundException, IOException
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main { /**/*from w ww . ja va 2 s . c om*/ * Copy a file from source to target. * * @param source * File * @param target * File * @throws FileNotFoundException * @throws IOException */ public static void copyFile(final File source, final File target) throws FileNotFoundException, IOException { FileChannel in = new FileInputStream(source).getChannel(), out = new FileOutputStream(target).getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while (in.read(buffer) != -1) { buffer.flip(); // Prepare for writing out.write(buffer); buffer.clear(); // Prepare for reading } out.close(); in.close(); } }