Java FileInputStream Copy copyFile(File oldFile, String newPath)

Here you can find the source of copyFile(File oldFile, String newPath)

Description

copy File

License

Apache License

Declaration

private static boolean copyFile(File oldFile, String newPath) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    private static boolean copyFile(File oldFile, String newPath) {
        if (!oldFile.exists() || !oldFile.isFile()) {
            return false;
        }//from   w ww .  j  ava 2s.  c om

        InputStream in = null;
        FileOutputStream fos = null;
        try {
            int len;
            in = new FileInputStream(oldFile);
            fos = new FileOutputStream(newPath);
            byte[] buffer = new byte[1024];
            while ((len = in.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return true;
    }
}

Related

  1. copyFile(File inputFile, File outputFile)
  2. copyFile(File inputFile, File outputFile)
  3. copyFile(File inputFile, OutputStream os)
  4. copyFile(File of, File nf)
  5. copyFile(File oldFile, File newFile)
  6. copyFile(File orig, File dest)
  7. copyFile(File orig, File dest, boolean overwrite)
  8. copyFile(File original, File copy)
  9. copyFile(File original, File destination)