Java Utililty Methods Copy File

List of utility methods to do Copy File

Description

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

Method

intcopyFiles(String p_source, String p_target)
copy Files
File sourceFile = null, targetFile = null;
BufferedOutputStream fout = null;
BufferedInputStream fin = null;
try {
    sourceFile = new File(p_source);
    if (!sourceFile.exists())
        return -1;
    targetFile = new File(p_target);
...
voidcopyFiles(String srcFilePath, String destFilePath)
This method is used for coping file from one place to the other.
FileInputStream input = null;
FileOutputStream output = null;
try {
    input = new FileInputStream(srcFilePath);
    output = new FileOutputStream(destFilePath);
    copyStreams(input, output);
} finally {
    if (input != null) {
...
voidcopyFiles(String srcFolderStr, String destFolderStr)
copy Files
copyFiles(srcFolderStr, destFolderStr, null);
voidcopyFiles(String srcPath, String destPath)
copy Files
File srcFile = new File(srcPath);
File destFile = new File(destPath);
destFile.mkdirs();
if (srcFile.isDirectory() && destFile.isDirectory())
    copyFiles(srcFile, destFile);
booleanCopyFiles(String strOrgFile, String strDestFile, boolean bFailIfExists)
Copy Files
File orgFile = new File(strOrgFile);
File destFile = new File(strDestFile);
if (!destFile.exists()) {
    if (copyFile(orgFile, destFile)) {
        return destFile.setLastModified(System.currentTimeMillis());
    } else {
        return false;
if (bFailIfExists) {
    return false;
if (strOrgFile.equalsIgnoreCase(strDestFile)) {
    return destFile.setLastModified(System.currentTimeMillis());
boolean bReadOnly = false;
if (!destFile.canWrite()) {
    bReadOnly = true;
    if (!setFileReadonly(strDestFile, false)) {
        return false;
if (copyFile(orgFile, destFile)) {
    if (destFile.setLastModified(System.currentTimeMillis())) {
        if (bReadOnly) {
            return destFile.setReadOnly();
        return true;
if (bReadOnly) {
    destFile.setReadOnly();
return false;
voidcopyFiles(String strPath, String dstPath, String srcExtension)
copy Files
File src = new File(strPath);
File dest = new File(dstPath);
if (src.isDirectory()) {
    dest.mkdirs();
    String list[] = src.list();
    for (int i = 0; i < list.length; i++) {
        String dest1 = dest.getAbsolutePath() + "\\" + list[i];
        String src1 = src.getAbsolutePath() + "\\" + list[i];
...
String[]copyFiles(String[] sourceFiles, String toDir, boolean overwrite)
Copy a list of files to a specifc folder.
String[] newFileNames = new String[sourceFiles.length];
for (int i = 0; i < sourceFiles.length; i++) {
    File inFile = new File(sourceFiles[i]);
    File outFile = new File(toDir + getSeparator() + inFile.getName());
    boolean fileExists = false;
    if (outFile.exists()) {
        fileExists = true;
        if (overwrite) {
...
voidcopyFileSafe(File destinationFileName, File sourceFileName)
copy File Safe
try {
    FileInputStream in = new FileInputStream(sourceFileName);
    FileOutputStream out = new FileOutputStream(destinationFileName);
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    in.close();
    out.close();
} catch (IOException e1) {
    e1.printStackTrace();
FilecopyFileSafe(File in, File out)
Copies a file from one location to another with exception suppression.
try {
    return copyFile(in, out);
} catch (IOException e) {
    System.err.println("Couldn't copy " + in + " to " + out + " (" + e + ")");
    return null;
voidcopyFilesBinary(String srcFile, String destDir)
Copies binary file originalFile to location toLocation If destination file exists it does nothing
File originalFile = new File(srcFile);
File toLocation = new File(destDir);
File destFile = new File(toLocation.getAbsolutePath() + File.separator + originalFile.getName());
if (destFile.exists())
    return;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
...