Here you can find the source of copyFile(File in, File out)
Parameter | Description |
---|---|
in | the file to copy from |
out | the file to copy to |
Parameter | Description |
---|---|
IOException | if a problem occurs when writing to the file |
public static void copyFile(File in, File out) throws IOException
//package com.java2s; //License from project: Apache License import java.io.*; import java.nio.channels.FileChannel; public class Main { /**/*from ww w . j a v a 2 s .c o m*/ * Copy the content of a file to another. * * @param in the file to copy from * @param out the file to copy to * * @throws IOException if a problem occurs when writing to the file */ public static void copyFile(File in, File out) throws IOException { copyFile(in, out, true); } /** * Copy the content of one file to another. * * @param in the file to copy from * @param out the file to copy to * @param overwrite boolean indicating whether out should be overwritten * * @throws IOException if a problem occurs when writing to the file */ public static void copyFile(File in, File out, boolean overwrite) throws IOException { long start = 0; if (out.exists() && out.length() > 0) { if (overwrite) { out.delete(); } else { start = out.length(); } } FileChannel inChannel = new FileInputStream(in).getChannel(); FileChannel outChannel = new FileOutputStream(out).getChannel(); try { inChannel.transferTo(start, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.close(); } } } }