Java FileInputStream Copy copyFile(final File source, final File dest)

Here you can find the source of copyFile(final File source, final File dest)

Description

copy File

License

Open Source License

Parameter

Parameter Description
source source.
dest destination.

Exception

Parameter Description

Declaration

public static void copyFile(final File source, final File dest) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   w w w  .j a v  a 2s  .co m*/
 * Copyright (c) {2011} {meter@rbtsb.com} {
 * individual contributors as indicated by the @authors tag}.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Private License v1.0
 * which accompanies this distribution, and is available at
 * http://www.rbtsb.com
 */

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 {
    /**
     * @param source
     *            source.
     * @param dest
     *            destination.
     * @throws Exception .
     */
    public static void copyFile(final File source, final File dest) throws IOException {
        if (!dest.getParentFile().exists()) {
            String dir = dest.getParentFile().toString();
            new File(dir).mkdir();
        }
        InputStream in = null;
        OutputStream out = null;
        if (source.exists()) {
            try {
                in = new FileInputStream(source);
                out = new FileOutputStream(dest);
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } finally {
                if (in != null)
                    in.close();
                if (out != null)
                    out.close();
            }
        }
    }
}

Related

  1. copyFile(final File from, final File to)
  2. copyFile(final File fromFile, final File toFile)
  3. copyFile(final File fromFile, final File toFile)
  4. copyFile(final File in, final File out)
  5. copyFile(final File oldfile, final File newfile)
  6. copyFile(final File source, final File target)
  7. copyFile(final File src, final File dest)
  8. copyFile(final File src, final File dst)
  9. copyFile(final File srcFile, final File destFile)