Here you can find the source of copyFile(File source, File dest)
Parameter | Description |
---|---|
source | a parameter |
dest | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void copyFile(File source, File dest) throws IOException
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class Main { private static final long MAX_COPY_COUNT = (64L * 1024L * 1024L) - (32L * 1024L); /**/*from ww w.j a v a2 s .co m*/ * Copies one file to another one. * * @param source * @param dest * * @throws IOException */ public static void copyFile(File source, File dest) throws IOException { if (dest.exists()) throw new IOException("Destination file already exists."); FileChannel in = null; FileChannel out = null; try { in = new FileInputStream(source).getChannel(); out = new FileOutputStream(dest).getChannel(); long size = in.size(); long position = 0L; while (position < size) position += in.transferTo(position, MAX_COPY_COUNT, out); dest.setLastModified(source.lastModified()); } finally { if (in != null) in.close(); if (out != null) out.close(); } } }