Copy a file and overwrite
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public final class FileUtil {
public static void copyFile(String from, String to) {
copyFile(from, to, Boolean.FALSE);
}
public static void copyFile(String from, String to, Boolean overwrite) {
try {
File fromFile = new File(from);
File toFile = new File(to);
if (!fromFile.exists()) {
throw new IOException("File not found: " + from);
}
if (!fromFile.isFile()) {
throw new IOException("Can't copy directories: " + from);
}
if (!fromFile.canRead()) {
throw new IOException("Can't read file: " + from);
}
if (toFile.isDirectory()) {
toFile = new File(toFile, fromFile.getName());
}
if (toFile.exists() && !overwrite) {
throw new IOException("File already exists.");
} else {
String parent = toFile.getParent();
if (parent == null) {
parent = System.getProperty("user.dir");
}
File dir = new File(parent);
if (!dir.exists()) {
throw new IOException("Destination directory does not exist: " + parent);
}
if (dir.isFile()) {
throw new IOException("Destination is not a valid directory: " + parent);
}
if (!dir.canWrite()) {
throw new IOException("Can't write on destination: " + parent);
}
}
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(fromFile);
fos = new FileOutputStream(toFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
} finally {
if (from != null) {
try {
fis.close();
} catch (IOException e) {
System.out.println(e);
}
}
if (to != null) {
try {
fos.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
} catch (Exception e) {
System.out.println("Problems when copying file.");
}
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
IO File:
- Compare File Dates
- Compress files using with ZIP
- Concatenate files
- Copy a File with NIO FileChannel and ByteBuffer
- Copy a file with FileReader and FileWriter
- Copy a file with InputStream and OutputStream
- Copy a file and overwrite
- Delete a file
- Delete File Recursively
- Get readable file size
- Move a file
- Rename a file
- Report a file's status
- Search a file by regular expressions
- Touch a file: set File Last Modified Time