Java FileInputStream Copy copyFile(File source, File target)

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

Description

Copies a file to a different location

License

Open Source License

Parameter

Parameter Description
source a parameter
target a parameter

Exception

Parameter Description
IOException Is thrown, if the source file does not exist

Declaration

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

Method Source Code

//package com.java2s;
/** /*w  w  w  .j  a va  2s .  c  o  m*/
 * Copyright (c) 2009-2011, The HATS Consortium. All rights reserved. 
 * This file is licensed under the terms of the Modified BSD License.
 */

import java.io.*;

public class Main {
    /**
     * Copies a file to a different location
     * @param source
     * @param target
     * @throws IOException Is thrown, if the source file does not exist
     */
    public static void copyFile(File source, File target) throws IOException {
        InputStream inputStream = new FileInputStream(source);
        OutputStream outputStream = new FileOutputStream(target);

        try {
            copyFile(inputStream, outputStream);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }

    /**
     * Copies an input stream into an output stream
     * @param inputStream
     * @param outputStream
     * @throws IOException
     */
    public static void copyFile(InputStream inputStream, OutputStream outputStream) throws IOException {
        int length;
        byte[] buffer = new byte[4096];
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
    }
}

Related

  1. copyFile(File source, File destination)
  2. copyFile(File source, File destination)
  3. copyFile(File source, File destination)
  4. copyFile(File source, File destination)
  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)