Java FileInputStream Copy copyFile(File in, File out)

Here you can find the source of copyFile(File in, File out)

Description

Copies a file from one location to antother

License

Apache License

Parameter

Parameter Description
in source
out target

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(File in, File out) throws IOException 

Method Source Code


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

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

public class Main {
    /**/*from   www. j  av  a 2  s. c  om*/
     * Copies a file from one location to antother
     * 
     * @param in source
     * @param out target
     * @throws IOException
     */
    public static void copyFile(File in, File out) throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(in);
            copyStreamToFile(fis, out);
        } finally {
            if (fis != null) {
                fis.close();
            }
        }

    }

    public static void copyStreamToFile(InputStream in, File out) throws IOException {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(out);
            byte[] buf = new byte[1024];
            int i = 0;
            while ((i = in.read(buf)) != -1) {
                fos.write(buf, 0, i);
            }
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

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 in, File out)
  7. copyFile(File in, File out)
  8. copyFile(File inFile, File outFile)
  9. copyFile(File inFile, File outFile)