Java FileInputStream Copy copyFile(final File oldfile, final File newfile)

Here you can find the source of copyFile(final File oldfile, final File newfile)

Description

copy File

License

Open Source License

Declaration

public static boolean copyFile(final File oldfile, final File newfile) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static boolean copyFile(final File oldfile, final File newfile) {
        FileInputStream fis;//from  ww w  . j a  v a  2  s  .c  om
        FileOutputStream fos;
        int len;
        byte[] buf;

        try {
            if (!newfile.exists()) {
                newfile.createNewFile();
            }

            if (!oldfile.canRead() || !newfile.canWrite()) {
                return false;
            }

            fis = new FileInputStream(oldfile);
            fos = new FileOutputStream(newfile);

            buf = new byte[4096];

            while ((len = fis.read(buf)) > 0) {
                fos.write(buf, 0, len);
            }

            fis.close();
            fos.close();

        } catch (final IOException e) {
            e.printStackTrace();
        }

        return true;
    }
}

Related

  1. copyFile(File targetFile, File file)
  2. copyFile(final File from, final File to)
  3. copyFile(final File fromFile, final File toFile)
  4. copyFile(final File fromFile, final File toFile)
  5. copyFile(final File in, final File out)
  6. copyFile(final File source, final File dest)
  7. copyFile(final File source, final File target)
  8. copyFile(final File src, final File dest)
  9. copyFile(final File src, final File dst)