Java FileInputStream Copy copyFile(File source, File target)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File source, File target) 

Method Source Code

//package com.java2s;
/*//from   www .  j  a  v a  2  s.  c o m
 * Copyright (c) 2014 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */

import java.io.Closeable;
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 {
    public static void copyFile(File source, File target) {
        int size = (int) source.length();
        byte[] buffer = new byte[size];
        InputStream in = null;

        try {
            in = new FileInputStream(source);
            in.read(buffer);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            close(in);
        }

        OutputStream out = null;

        try {
            out = new FileOutputStream(target);
            out.write(buffer);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } finally {
            close(out);
        }
    }

    public static void close(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

Related

  1. copyFile(File source, File destination)
  2. copyFile(File source, File destination)
  3. copyFile(File source, File target)
  4. copyFile(File source, File target)
  5. copyFile(File source, File target)
  6. copyFile(File source, File target)
  7. copyFile(File source, File target)
  8. copyFile(File source, File target)
  9. copyFile(File source, File target)