Here you can find the source of copyFileToFile(File file, File destFile, boolean overwrite)
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. |
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. |
public static void copyFileToFile(File file, File destFile, boolean overwrite) throws IOException
//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()); } }