Java FileInputStream Copy copyFile(String sourceFile, String targetFile)

Here you can find the source of copyFile(String sourceFile, String targetFile)

Description

copy File

License

Open Source License

Declaration

public static boolean copyFile(String sourceFile, String targetFile) 

Method Source Code


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

import java.io.FileOutputStream;

public class Main {

    public static boolean copyFile(String sourceFile, String targetFile) {
        File file1 = new File(sourceFile);
        File file2 = new File(targetFile);
        FileInputStream fis = null;
        FileOutputStream fos = null;

        boolean isOK = true;

        try {//from  www. j  a  v  a  2 s .  c o m
            if (file1.exists()) {
                if (!file2.exists()) {
                    file2.createNewFile();

                }
                fis = new FileInputStream(file1);
                byte[] buffer = new byte[(int) file1.length()];
                fis.read(buffer);
                fos = new FileOutputStream(file2);
                fos.write(buffer);
            } else {
                isOK = false;
            }
        } catch (Exception e) {
            System.out.println(e);
            isOK = false;
        } finally {
            try {
                fis.close();
            } catch (Exception ex) {
            }
            try {
                fos.close();
            } catch (Exception ex) {
            }
        }
        return isOK;
    }
}

Related

  1. copyFile(String source, String destination)
  2. copyFile(String source, String target)
  3. copyFile(String source, String targetDirectory, String filesep)
  4. copyFile(String sourceFile, String destDir, String newFileName)
  5. copyFile(String sourceFile, String destFile)
  6. copyFile(String sourcefile, String targetfile)
  7. copyFile(String sourcefile, String targetFile)
  8. copyFile(String sourceFile, String targetFolder)
  9. copyFile(String sourceFile, String targetFolder)