Java FileInputStream Copy copyFile(File in, File out)

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

Description

Copy a file from one place to another

License

Open Source License

Parameter

Parameter Description
in a parameter
out a parameter

Exception

Parameter Description
Exception an exception

Declaration

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

Method Source Code

//package com.java2s;
/**//  www. ja v  a 2  s  .  co m
 * Copyright (c) 2012 Todoroo Inc
 *
 * See the file "LICENSE" for the full license governing this code.
 */

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

import java.io.OutputStream;

public class Main {
    /**
     * Copy a file from one place to another
     *
     * @param in
     * @param out
     * @throws Exception
     */
    public static void copyFile(File in, File out) throws Exception {
        FileInputStream fis = new FileInputStream(in);
        FileOutputStream fos = new FileOutputStream(out);
        try {
            copyStream(fis, fos);
        } catch (Exception e) {
            throw e;
        } finally {
            fis.close();
            fos.close();
        }
    }

    /**
     * Copy stream from source to destination
     * @param source
     * @param dest
     * @throws IOException
     */
    public static void copyStream(InputStream source, OutputStream dest) throws IOException {
        int bytes;
        byte[] buffer;
        int BUFFER_SIZE = 1024;
        buffer = new byte[BUFFER_SIZE];
        while ((bytes = source.read(buffer)) != -1) {
            if (bytes == 0) {
                bytes = source.read();
                if (bytes < 0)
                    break;
                dest.write(bytes);
                dest.flush();
                continue;
            }

            dest.write(buffer, 0, bytes);
            dest.flush();
        }
    }
}

Related

  1. copyFile(File from, File toFile)
  2. copyFile(File fromFile, File toFile)
  3. copyFile(File fromFile, File toFile)
  4. copyFile(File fromFile, File toFile)
  5. copyFile(File fromFile, File toFile, IProgressMonitor monitor)
  6. copyFile(File in, File out)
  7. copyFile(File in, File out)
  8. copyFile(File in, File out)
  9. copyFile(File in, File out)