Java Utililty Methods File Copy

List of utility methods to do File Copy

Description

The list of methods to do File Copy are organized into topic(s).

Method

voidcopyFiles(File src, File dest, boolean override)
copy Files
copyFiles(src, dest, override, null);
voidcopyFiles(File src, File dest, FilenameFilter filter)
copy Files
File[] files = src.listFiles(filter);
if (files == null) {
    System.err.println("copyFiles: No files in src directory " + src);
    return;
for (int i = 0; i < files.length; i++) {
    File srcFile = files[i];
    File destFile = new File(dest, srcFile.getName());
...
voidcopyFiles(File src, File dest, String... filenameExtension)
This method copies, from a given directory, a set of files with a given list of file extensions.
List<File> files = new ArrayList<File>();
if (src.isDirectory() == false) {
    return;
} else {
    files.add(src);
    while (files.isEmpty() == false) {
        File tmpFile = files.remove(files.size() - 1);
        for (File subFile : tmpFile.listFiles()) {
...
voidcopyFiles(File srcDir, File destDir, String... regexps)
Copies files from source to destination dir.
if (!(srcDir.exists() && srcDir.isDirectory()))
    throw new IllegalArgumentException("Source directory doesn't exist: " + srcDir.getAbsolutePath());
if (!(destDir.exists() && destDir.isDirectory()))
    throw new IllegalArgumentException("Destination directory doesn't exist: " + destDir.getAbsolutePath());
File[] files = srcDir.listFiles();
for (File file : files) {
    if (file.isFile()) {
        String name = file.getName();
...
voidcopyFiles(File srcFile, File destFolder)
copy Files
InputStream in = new FileInputStream(srcFile);
if (!destFolder.exists()) {
    destFolder.mkdirs();
File destFile = new File(destFolder, srcFile.getName());
OutputStream out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int length;
...
voidcopyFiles(InputStream inStream, FileOutputStream outStream)
copy Files
byte[] buffer = new byte[1024];
int length;
while ((length = inStream.read(buffer)) > 0) {
    outStream.write(buffer, 0, length);
voidcopyFiles(String from, String to)
copy Files
File file = new File(from);
File toFolder = new File(to);
if (!file.exists() || !file.isDirectory()) {
    return;
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
    File child = new File(toFolder, files[i].getName());
...
booleancopyFiles(String fromDirName, String toDirName)
Copy all the files under fromDirName to toDirName
boolean res = true;
File fromDir = new File(fromDirName);
if (!fromDir.exists() || !fromDir.isDirectory() || !fromDir.canRead()) {
    System.err.println(fromDir.getAbsolutePath() + "doesn't exist, or isn't file, or can't be read");
    return false;
File[] files = fromDir.listFiles();
for (int i = 0; i < files.length; i++) {
...
booleancopyFileToPath(String fileToPath, String fileName, String sourceFile)
copy File To Path
try {
    File dic = new File(fileToPath);
    if (!dic.exists()) {
        dic.mkdirs();
    InputStream is = new BufferedInputStream(new FileInputStream(sourceFile));
    OutputStream os = new BufferedOutputStream(new FileOutputStream(fileToPath + "/" + fileName));
    byte[] bytes = new byte[1024];
...
voiddoCopyFile(File srcFile, File destFile, boolean preserveFileDate)
do Copy File
if ((destFile.exists()) && (destFile.isDirectory())) {
    throw new IOException("Destination '" + destFile + "' exists but is a directory");
FileInputStream input = new FileInputStream(srcFile);
try {
    FileOutputStream output = new FileOutputStream(destFile);
    try {
        copy(input, output);
...