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

Declaration

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

Method Source Code

//package com.java2s;
/*/*w  w  w. j  ava 2 s .  co  m*/
 * Copyright 2015, Yahoo Inc.
 * Copyrights licensed under the Apache 2.0 License.
 * See the accompanying LICENSE file for terms.
 */

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
     */
    public static void copyFile(File in, File out) throws IOException {
        FileInputStream fis = new FileInputStream(in);
        FileOutputStream fos = new FileOutputStream(out);
        try {
            copyStream(fis, fos);
        } finally {
            fis.close();
            fos.close();
        }
    }

    /**
     * Copy a stream from source to destination
     */
    private 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)) > 0) {
            dest.write(buffer, 0, bytes);
        }
    }
}

Related

  1. copyFile(File fromFile, File toFile, IProgressMonitor monitor)
  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 in, File out)
  9. copyFile(File inFile, File outFile)