Java FileInputStream Copy copyFile(File inFile, File outFile)

Here you can find the source of copyFile(File inFile, File outFile)

Description

Copy a file

License

Open Source License

Parameter

Parameter Description
inFile src file
outFile dest file

Return

true if the operation succeeded

Declaration

public static boolean copyFile(File inFile, File outFile) 

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 {
    private static final int BUFFERSIZE = 8192;

    /**/*from  w w  w .  j  av  a  2 s.c  om*/
     * Copy a file
     * @param inFile src file
     * @param outFile dest file
     * @return true if the operation succeeded
     */
    public static boolean copyFile(File inFile, File outFile) {
        FileInputStream in = null;
        FileOutputStream out = null;
        boolean success = false;

        try {
            in = new FileInputStream(inFile);
            out = new FileOutputStream(outFile);
            byte[] buffer = new byte[BUFFERSIZE];
            int n;

            while ((n = in.read(buffer)) != -1) {
                out.write(buffer, 0, n);
            }
            out.flush();
            success = true;
        } catch (IOException e) {
            System.err.println(
                    "Error in copying file " + inFile.getAbsolutePath() + " to " + outFile.getAbsolutePath());
            System.err.println(e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    System.err.println(e);
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    System.err.println(e);
                }
            }
        }

        return success;
    }
}

Related

  1. copyFile(File in, File out)
  2. copyFile(File in, File out)
  3. copyFile(File in, File out)
  4. copyFile(File in, File out)
  5. copyFile(File in, File out)
  6. copyFile(File inFile, File outFile)
  7. copyFile(File input, File output)
  8. copyFile(File inputFile, File outputFile)
  9. copyFile(File inputFile, File outputFile)