Here you can find the source of copy(File source, File dest, boolean preserveTime)
source
to location dest
.
public static void copy(File source, File dest, boolean preserveTime) throws IOException
//package com.java2s; /******************************************************************************* * ALMA - Atacama Large Millimeter Array * Copyright (c) ESO - European Southern Observatory, 2011 * (in the framework of the ALMA collaboration). * All rights reserved.// ww w.j ava 2 s .c om * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *******************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class Main { /** * Copies file <code>source</code> to location <code>dest</code>. * Necessary directories are created automatically. * The modification time is preserved if <code>preserveTime</code> is <code>true</code>. */ public static void copy(File source, File dest, boolean preserveTime) throws IOException { FileChannel in = null, out = null; try { dest.getParentFile().mkdirs(); in = new FileInputStream(source).getChannel(); out = new FileOutputStream(dest).getChannel(); long size = in.size(); MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size); out.write(buf); if (preserveTime) { dest.setLastModified(source.lastModified()); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }