Java FileInputStream Copy copyFile(String original, String copy)

Here you can find the source of copyFile(String original, String copy)

Description

Copies a file from one location to another.

License

Open Source License

Parameter

Parameter Description
original The source file.
copy The destination file.

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(String original, String copy) throws IOException 

Method Source Code


//package com.java2s;
/*/*w  w  w  . j  a v  a 2s .c  om*/
    
Copyright: 2010 Bindley Bioscience Center, Purdue University
    
License: X11 license.
    
  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:
    
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
    
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
    
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;

public class Main {
    /**
     * Copies a file from one location to another.
     * 
     * @param original The source file.
     * @param copy The destination file.
     * @throws IOException
     */
    public static void copyFile(File original, File copy) throws IOException {
        FileInputStream in = new FileInputStream(original);
        copy.getParentFile().mkdirs();
        FileOutputStream out = new FileOutputStream(copy);
        copyStream(in, out);
        in.close();
        out.close();
    }

    /**
     * Copies a file from one location to another.
     * 
     * @param original The source file.
     * @param copy The destination file.
     * @throws IOException
     */
    public static void copyFile(String original, String copy) throws IOException {
        copyFile(new File(original), new File(copy));
    }

    /**
     * Copies from one stream to another.
     * 
     * @param original The source stream.
     * @param copy The destination stream.
     * @throws IOException
     */
    public static void copyStream(InputStream original, OutputStream copy) throws IOException {
        byte[] buffer = new byte[8192];
        int length;
        while ((length = original.read(buffer)) > 0) {
            copy.write(buffer, 0, length);
        }
    }
}

Related

  1. copyFile(String inputFile, String outputFile)
  2. copyFile(String oldFile, String newFile)
  3. copyFile(String oldPath, String newPath)
  4. copyFile(String oldPath, String newPath)
  5. copyFile(String oldPathFile, String newPathFile)
  6. copyFile(String pathOld, String pathNew)
  7. copyFile(String pathOrig, String pathDst)
  8. copyFile(String quelle, String ziel)
  9. copyFile(String resourceFimeName, String targetFileName)