Java File Copy copyFile(File src, File dest, Component parentComponent, String message)

Here you can find the source of copyFile(File src, File dest, Component parentComponent, String message)

Description

copy File

License

LGPL

Declaration

public static void copyFile(File src, File dest,
            Component parentComponent, String message) throws IOException 

Method Source Code

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

import java.io.*;
import javax.swing.ProgressMonitorInputStream;

import java.awt.Component;

public class Main {
    /**/*from   w  w  w . j  a  v a 2  s .  c  om*/
     * The buffer size in used for the copy actions. Defaults to
     * <tt>65536</tt> bytes (64 kB).
     */
    public static int BUFFER_SIZE = 65536;

    public static void copyFile(File src, File dest,
            Component parentComponent, String message) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);
        copyFile(in, out, parentComponent, message);
        out.flush();
        out.close();
        in.close();
        dest.setLastModified(src.lastModified());
    }

    public static void copyFile(String src, String dest,
            Component parentComponent, String message) throws IOException {
        copyFile(new File(src), new File(dest), parentComponent, message);
    }

    /**
     * Copies the whole <tt>InputStream</tt> from <tt>src</tt> to the
     * <tt>OutputStream</tt> in <tt>out</tt>. The streams will be
     * buffered, and a progress monitor with <tt>message</tt> will be
     * displayed if the action takes a lot of time.
     *
     * <p>The <tt>OutputStream</tt> and the <tt>InputStream</tt> are
     * <i>not</i> closed and flushed after copying has finished.</p>
     *
     * @param parentComponent the parent component the progress dialog
     * will belong to (e.g. a <tt>Frame</tt> object).
     */
    public static void copyFile(InputStream src, OutputStream dest,
            Component parentComponent, String message) throws IOException {
        InputStream in = new BufferedInputStream(
                new ProgressMonitorInputStream(parentComponent, message,
                        src));
        BufferedOutputStream out = new BufferedOutputStream(dest);

        byte[] buffer = new byte[BUFFER_SIZE];

        int bytesRead = 0;
        while (bytesRead != -1) {
            bytesRead = in.read(buffer);
            if (bytesRead != -1) {
                out.write(buffer, 0, bytesRead);
            }
        }

        out.flush();
    }
}

Related

  1. copy(File from, File to)
  2. copyFile(File source, File target)
  3. copyFileFromFileSystem(String sourceFileName, File target)
  4. copyFiles(File file, File destPath)
  5. copyFiles(File fromDir, File toDir)
  6. copyFiles(File source, File destination)