Java Copy File copyFilesBinary(String srcFile, String destDir)

Here you can find the source of copyFilesBinary(String srcFile, String destDir)

Description

Copies binary file originalFile to location toLocation If destination file exists it does nothing

License

Open Source License

Parameter

Parameter Description
originalFile source file location
toLocation destination dir

Exception

Parameter Description
IOException exception

Declaration

public static void copyFilesBinary(String srcFile, String destDir) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**/*  w w  w.  j  ava2 s . c  o m*/
     * Copies binary file originalFile to location toLocation
     * If destination file exists it does nothing
     * 
     * @param originalFile source file location
     * @param toLocation destination dir 
     * @throws IOException exception
     */
    public static void copyFilesBinary(String srcFile, String destDir) throws IOException {

        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 {
            fis = new FileInputStream(originalFile);
            fos = new FileOutputStream(new File(toLocation, originalFile.getName()));
            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead); // write
            }

        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    // do nothing
                }
            }
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    // do nothing
                }
            }
        }
    }
}

Related

  1. CopyFiles(String strOrgFile, String strDestFile, boolean bFailIfExists)
  2. copyFiles(String strPath, String dstPath, String srcExtension)
  3. copyFiles(String[] sourceFiles, String toDir, boolean overwrite)
  4. copyFileSafe(File destinationFileName, File sourceFileName)
  5. copyFileSafe(File in, File out)
  6. copyFilesBinaryRecursively(File fromLocation, File toLocation, FileFilter fileFilter)
  7. copyFilesFromDirToDir(String originalDir, String targetDir)
  8. copyFilesRecursive(File src, File dest)
  9. copyFilesToLocal(String destination, String source)