Java FileInputStream Copy copyFile(File source, File destination)

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

Description

Copies a file;

License

Apache License

Parameter

Parameter Description
source a parameter
destination a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(File source, File destination) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  w  w w .  ja  v  a  2s  .c o m*/
 * Copyright 2011 cruxframework.org.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileInputStream;

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

public class Main {
    /**
     * Copies a file;
     * @param source
     * @param destination
     * @throws IOException
     */
    public static void copyFile(File source, File destination) throws IOException {
        FileOutputStream out = null;
        FileInputStream in = null;

        try {
            destination.getParentFile().mkdirs();
            out = new FileOutputStream(destination);
            in = new FileInputStream(source);
            byte[] buff = new byte[1024];
            int read = 0;
            while ((read = in.read(buff)) > 0) {
                out.write(buff, 0, read);
            }
            out.flush();
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * 
     * @param file
     * @return
     * @throws IOException
     */
    public static String read(File file) throws IOException {
        return read(new FileInputStream(file));
    }

    /**
     * @param in
     * @return
     * @throws IOException
     */
    public static String read(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int read = 0;
        byte[] buff = new byte[1024];
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }
        in.close();
        out.flush();
        out.close();

        return new String(out.toByteArray());
    }

    /**
     * @param text
     * @param f
     * @throws IOException
     */
    public static void write(String text, File f) throws IOException {
        FileOutputStream out = new FileOutputStream(f);
        out.write(text.getBytes());
        out.close();
    }

    /**
     * @param in
     * @param f
     * @throws IOException
     */
    public static void write(InputStream in, File f) throws IOException {
        FileOutputStream out = new FileOutputStream(f);

        int read = 0;
        byte[] buff = new byte[1024];
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }
        in.close();
        out.close();
    }
}

Related

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