Java File Copy fileCopy(String oldFilePath, String newFilePath, boolean isCover)

Here you can find the source of fileCopy(String oldFilePath, String newFilePath, boolean isCover)

Description

file Copy

License

Apache License

Declaration

public static boolean fileCopy(String oldFilePath, String newFilePath, boolean isCover) 

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 fileCopy(String oldFilePath, String newFilePath, boolean isCover) {
        if (!checkFile(oldFilePath, false)) {
            return false;
        }//from   w ww . ja  va 2  s .  c o m
        if (checkFile(newFilePath, true) && (!isCover)) {
            return false;
        }
        FileInputStream in = null;
        FileOutputStream out = null;
        byte[] buff = new byte[4096];
        try {
            in = new FileInputStream(oldFilePath);
            out = new FileOutputStream(newFilePath);

            int len = 0;
            while ((len = in.read(buff)) > 0) {
                out.write(buff, 0, len);
            }
        } catch (FileNotFoundException e) {
            return false;
        } catch (IOException e) {
            return false;
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
            }
        }
        return true;
    }

    public static boolean checkFile(String file, boolean create) {
        File f = new File(file);

        if (f.getParent() != null) {
            File fpath = new File(f.getParent());
            if (!fpath.exists()) {
                if (create) {
                    fpath.mkdirs();
                } else {
                    return false;
                }
            }
        }
        if (!f.exists()) {
            if (create) {
                try {
                    f.createNewFile();
                } catch (IOException e) {
                    return false;
                }
            } else {
                return false;
            }
        }
        return true;
    }
}

Related

  1. fileCopy(File srcFile, File tarFile)
  2. FileCopy(InputStream input, OutputStream output, int bufferSize)
  3. fileCopy(String from, String to)
  4. fileCopy(String from, String to)
  5. fileCopy(String fromPath, String toPath)
  6. fileCopy(String sFrom, String sTo)
  7. fileCopy(String source, String target)
  8. fileCopy(String sourceFile, String destinationFile)
  9. fileCopy(String sourcefile, String destinctionFile)