Java FileInputStream Copy copyFile(File src, File dst, boolean copyLMD)

Here you can find the source of copyFile(File src, File dst, boolean copyLMD)

Description

Copies a file; silently overwrites the destination if already exists.

License

Apache License

Parameter

Parameter Description
copyLMD tells if the last modification time of the original file will be copied too.

Declaration

public static void copyFile(File src, File dst, boolean copyLMD) throws IOException 

Method Source Code


//package com.java2s;
/*//www. j a  v a2  s .  com
 * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

public class Main {
    /**
     * Copies a file; silently overwrites the destination if already exists.
     * 
     * @param copyLMD tells if the last modification time of the original
     *     file will be copied too.
     */
    public static void copyFile(File src, File dst, boolean copyLMD) throws IOException {
        byte[] buffer = new byte[1024 * 64];
        InputStream in = new FileInputStream(src);
        try {
            long srcLMD = 0L;
            if (copyLMD) {
                srcLMD = src.lastModified();
                if (srcLMD == 0) {
                    throw new IOException(
                            "Failed to get the last modification " + "time of " + src.getAbsolutePath());
                }
            }
            OutputStream out = new FileOutputStream(dst);
            try {
                int ln;
                while ((ln = in.read(buffer)) != -1) {
                    out.write(buffer, 0, ln);
                }
            } finally {
                out.close();
            }
            if (srcLMD != 0L) {
                dst.setLastModified(srcLMD);
            }
        } finally {
            in.close();
        }
    }

    /**
     * Same as {@link #copyFile(File, File, boolean) copyFile(src, dst, true))}.
     */
    public static void copyFile(File src, File dst) throws IOException {
        copyFile(src, dst, true);
    }
}

Related

  1. copyFile(File src, File dst)
  2. copyFile(File src, File dst)
  3. copyFile(File src, File dst)
  4. copyFile(File src, File dst)
  5. copyFile(File src, File dst)
  6. copyFile(File src, File target)
  7. copyFile(File src, File toDir)
  8. copyFile(File srcFile, File destFile)
  9. copyFile(File srcFile, File destFile)