Java FileInputStream Copy copyFileBin(File sourceFile, File targetFile)

Here you can find the source of copyFileBin(File sourceFile, File targetFile)

Description

copy File Bin

License

Apache License

Declaration

public static boolean copyFileBin(File sourceFile, File targetFile) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;

public class Main {
    public static boolean copyFileBin(File sourceFile, File targetFile) {
        // check deltas
        boolean deltas = (targetFile.lastModified() != sourceFile
                .lastModified());/*from w ww .  ja  v a  2s .com*/
        if (!deltas) {
            deltas = deltas || (targetFile.length() != sourceFile.length());
        }

        if (deltas) {
            if (!targetFile.exists()) {
                // create parent directory if necessary
                File parentDir = targetFile.getParentFile();
                if (!parentDir.isDirectory()) {
                    if (!parentDir.mkdirs()) {
                        return false;
                    }
                }
            } else {
                targetFile.delete();
            }
            try {
                targetFile.createNewFile();
            } catch (IOException e) {
                return false;
            }

            FileOutputStream fileO;
            try {
                fileO = new FileOutputStream(targetFile.getAbsolutePath(),
                        true);
            } catch (FileNotFoundException e) {
                return false;
            }
            FileInputStream fileI;
            try {
                fileI = new FileInputStream(sourceFile);
            } catch (FileNotFoundException e) {
                return false;
            }
            try {
                byte[] buf = new byte[1024];
                int len;
                while ((len = fileI.read(buf)) > 0) {
                    fileO.write(buf, 0, len);
                }
                fileI.close();

                fileO.flush();
                fileO.close();
            } catch (IOException e) {
                try {
                    fileI.close();
                } catch (IOException e1) {
                    // ignore

                }
                try {
                    fileO.close();
                } catch (IOException e1) {
                    // ignore

                }
                return false;
            }

            targetFile.setLastModified(sourceFile.lastModified());
        }
        return true;
    }
}

Related

  1. copyFile(String srFile, String dtFile)
  2. copyFile(String srFilePath, String dtFilePath)
  3. copyFile(String target, String source)
  4. copyFileAndReName(String destPath, String srcPath, String srcFile, String destFile)
  5. copyFileAsStream(String srcFilePathName, String dstFilePathName)
  6. copyFileBinary(String source, String destination)
  7. copyFileContents(File fromFile, File destination, boolean useTempFilenames)
  8. copyFileFolder(final File source, final File dest, final boolean override)
  9. copyFileFromFileToFile(File fromFile, File toFile)