Java FileInputStream Copy copyFileToFile(File file, File destFile, boolean overwrite)

Here you can find the source of copyFileToFile(File file, File destFile, boolean overwrite)

Description

Copy a file to another file.

License

Open Source License

Parameter

Parameter Description
file The file to copy.
destFile The file to copy the file to. If <code>overwrite</code> is <code>false</code> it must not exist yet. In either case it may not be an existing directory.
overwrite Whether <code>destFile</code> should be overwritten if it already exists. If this is false and the file does already exist an IllegalStateException will be thrown.

Exception

Parameter Description
IOException If there is an I/O error while performing the copy.
IllegalStateException If <code>overwrite</code> was<code>false</code> and <code>destFile</code> already existed.

Declaration

public static void copyFileToFile(File file, File destFile, boolean overwrite) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    private static final int BUFFER_SIZE = 32768;

    /**//from   w ww .ja  va 2 s .c om
     * Copy a file to another file.
     *
     * @param file The file to copy.
     * @param destFile The file to copy the file to. If <code>overwrite</code>
     *                 is <code>false</code> it must not exist yet. In either
     *                 case it may not be an existing directory.
     * @param overwrite Whether <code>destFile</code> should be overwritten if
     *                  it already exists. If this is false and the file does
     *                  already exist an {@link IllegalStateException} will be
     *                  thrown.
     * @throws IOException If there is an I/O error while performing the copy.
     * @throws IllegalStateException If <code>overwrite</code> was
     * <code>false</code> and <code>destFile</code> already existed.
     */
    public static void copyFileToFile(File file, File destFile, boolean overwrite) throws IOException {
        if ((!overwrite) && destFile.isFile()) {
            throw new IllegalStateException("Destination file " + destFile + " already exists");
        }
        if (destFile.isDirectory()) {
            throw new IllegalStateException("Destination file is an existing directory");
        }
        try (FileInputStream in = new FileInputStream(file);
                FileOutputStream out = new FileOutputStream(destFile)) {
            int bytesRead;
            byte[] buffer = new byte[BUFFER_SIZE];
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
        }
        destFile.setLastModified(file.lastModified());
    }
}

Related

  1. copyFileToDestDir(String srcFilePath, String destFileDir)
  2. copyFileToDir(File oldFile, File outputDir)
  3. copyFileToDir(File sourceFile, File destDir)
  4. copyFileToDir(String targetDir, String[] filePath)
  5. CopyFileToDirectory(String file_name, String from_directory, String to_directory)
  6. copyFileToFile(File input, String outputPath, String exportName)
  7. copyFileToOutputStream(File file, OutputStream stream)
  8. copyFileToOutputStream(String fileLocation, OutputStream os)
  9. copyFileToStream(File currentFile, OutputStream outputStream)