Java FileInputStream Copy copyFile(File fileSrc, File fileDest)

Here you can find the source of copyFile(File fileSrc, File fileDest)

Description

copy File

License

Open Source License

Declaration

private static boolean copyFile(File fileSrc, File fileDest) 

Method Source Code

//package com.java2s;
/*/* w  ww .  j  a v  a  2  s.c  o m*/
 * 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 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;
    }
}

Related

  1. copyFile(File existingFile, File destFile)
  2. copyFile(File file, File destPath)
  3. copyFile(File file, File dir)
  4. copyFile(File file, File newFile, boolean overwrite, boolean setLastModified)
  5. copyFile(File file, OutputStream os)
  6. copyFile(File fileToCopy, File targetDir)
  7. copyFile(File from, File to)
  8. copyFile(File from, File to)
  9. copyFile(File from, File to)