Description
Copies the content of a file to another file.
License
Open Source License
Parameter
Parameter | Description |
---|
file | the file to be copied |
newFile | the file to copy to |
overwrite | if false, the file is not copied if newFile exists |
setLastModified | if true, newFile gets the same date of file |
Exception
Parameter | Description |
---|
IOException | if the content can't be copied due to an I/O error |
Return
true if copied successfully, false otherwise
Declaration
public static boolean copyFile(File file, File newFile, boolean overwrite, boolean setLastModified)
throws IOException
Method Source Code
//package com.java2s;
/*/*w w w. j av a 2s.c om*/
* Copyright 2004-2009 Luciano Vernaschi
*
* This file is part of MeshCMS.
*
* MeshCMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MeshCMS 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MeshCMS. If not, see <http://www.gnu.org/licenses/>.
*/
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 {
/**
* A standard size for buffers.
*/
public static final int BUFFER_SIZE = 2048;
/**
* Copies the content of a file to another file.
*
* @param file the file to be copied
* @param newFile the file to copy to
* @param overwrite if false, the file is not copied if newFile exists
* @param setLastModified if true, newFile gets the same date of file
*
* @return true if copied successfully, false otherwise
* @throws IOException if the content can't be copied due to an I/O error
*/
public static boolean copyFile(File file, File newFile, boolean overwrite, boolean setLastModified)
throws IOException {
if (newFile.exists() && !overwrite) {
return false;
}
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
copyStream(fis, new FileOutputStream(newFile), true);
if (setLastModified) {
newFile.setLastModified(file.lastModified());
}
} finally {
if (fis != null) {
fis.close();
}
}
return true;
}
/**
* Reads an <code>InputStream</code> and copy all data to an
* <code>OutputStream</code>.
*
* @param in the Input Stream
* @param out the Output Stream
* @param closeOut if true, out is closed when the copy has finished
*
* @throws IOException if an I/O error occurs.
*/
public static void copyStream(InputStream in, OutputStream out, boolean closeOut) throws IOException {
byte b[] = new byte[BUFFER_SIZE];
int n;
try {
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
} finally {
try {
in.close();
} finally {
if (closeOut) {
out.close();
}
}
}
}
}
Related
- copyFile(File destFile, File srcFile)
- copyFile(File destination, File source)
- copyFile(File existingFile, File destFile)
- copyFile(File file, File destPath)
- copyFile(File file, File dir)
- copyFile(File file, OutputStream os)
- copyFile(File fileSrc, File fileDest)
- copyFile(File fileToCopy, File targetDir)
- copyFile(File from, File to)