Java File Copy nio copy(File source, File dest, boolean preserveTime)

Here you can find the source of copy(File source, File dest, boolean preserveTime)

Description

Copies file source to location dest.

License

Open Source License

Declaration

public static void copy(File source, File dest, boolean preserveTime) throws IOException 

Method Source Code

//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();
            }
        }
    }
}

Related

  1. copy(File source, File dest)
  2. copy(File source, File dest)
  3. copy(File source, File dest)
  4. copy(File source, File dest)
  5. copy(File source, File dest)
  6. copy(File source, File destination)
  7. copy(File source, File destination)
  8. copy(File source, File destination)
  9. copy(File source, File destination)