Java Copy File CopyFiles(String strOrgFile, String strDestFile, boolean bFailIfExists)

Here you can find the source of CopyFiles(String strOrgFile, String strDestFile, boolean bFailIfExists)

Description

Copy Files

License

Open Source License

Declaration

private static boolean CopyFiles(String strOrgFile, String strDestFile, boolean bFailIfExists) 

Method Source Code

//package com.java2s;
/*//  ww w.j  av a2 s.  com
 * MergeHero: Differing and Merging Folders & Files
 *
 * Copyright ? 2004, Dynamsoft, Inc. All rights reserved.
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see http://www.opensource.org/licenses/gpl-3.0.html.
 */

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    private static boolean CopyFiles(String strOrgFile, String strDestFile, boolean bFailIfExists) {
        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;
    }

    private static boolean copyFile(File fileSrc, File fileDest) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            fileInputStream = new FileInputStream(fileSrc);
            fileOutputStream = new FileOutputStream(fileDest);

            int iBufferSize = 1024 * 1024; //Buffer Size: 1M
            byte byaryBuffer[] = new byte[iBufferSize];

            for (int i = 0; i < fileSrc.length() / iBufferSize; i++) //if the file size big than 1M
            {
                fileInputStream.read(byaryBuffer);

                fileOutputStream.write(byaryBuffer);
            }

            byaryBuffer = null;

            int iNotReadLength = (int) (fileSrc.length() % iBufferSize);

            if (iNotReadLength > 0) {
                byte byaryNotRead[] = new byte[iNotReadLength];

                fileInputStream.read(byaryNotRead);
                fileOutputStream.write(byaryNotRead);

                byaryNotRead = null;
            }

            try {
                fileInputStream.close();
                fileInputStream = null;
                fileOutputStream.close();
                fileOutputStream = null;
            } catch (IOException ex) {
                return false;
            }

            return true;
        } catch (Exception e) {
            try {
                if (fileInputStream != null)
                    fileInputStream.close();

                if (fileOutputStream != null)
                    fileOutputStream.close();
            } catch (IOException ex) {
            } //try
        } //try

        return false;
    }

    private static boolean setFileReadonly(String strLocalFileFullName, boolean bReadOnly) {
        File fileToSet = new File(strLocalFileFullName);

        if (fileToSet.exists() && fileToSet.isFile()) {
            if (bReadOnly) {
                return fileToSet.setReadOnly();
            } else {
                try {
                    //backup the modification time of the file to set attribute
                    long lTimeModification = fileToSet.lastModified();

                    //create a temp file
                    long lTime = System.currentTimeMillis();
                    File fileTemp = File.createTempFile("~~~saw" + String.valueOf(lTime), ".TMP");

                    //copy the content of the file(which attribute will be set) to the temp file
                    copyFile(fileToSet, fileTemp);

                    //delete the file(which attriabute will be set)
                    fileToSet.delete();

                    //create a file which name is the same to the deleted files,
                    //after creating ,the file attribute is writable
                    fileToSet.createNewFile();

                    //copy the content of the temp to the file to set 
                    copyFile(fileTemp, fileToSet);

                    //Delete the temp file
                    fileTemp.delete();

                    //Set the buckup modification time to the file
                    fileToSet.setLastModified(lTimeModification);

                    return true;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    //e.printStackTrace();
                } //try
            } //if
        } //if   

        return false;
    }
}

Related

  1. copyFiles(String p_source, String p_target)
  2. copyFiles(String srcFilePath, String destFilePath)
  3. copyFiles(String srcFolderStr, String destFolderStr)
  4. copyFiles(String srcPath, String destPath)
  5. copyFiles(String strPath, String dstPath, String srcExtension)
  6. copyFiles(String[] sourceFiles, String toDir, boolean overwrite)
  7. copyFileSafe(File destinationFileName, File sourceFileName)
  8. copyFileSafe(File in, File out)