Java FileInputStream Copy copyFile(File targetFile, File file)

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

Description

Copy file to file

License

Apache License

Parameter

Parameter Description
targetFile a parameter
file a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(File targetFile, File file) throws IOException 

Method Source Code

//package com.java2s;
/**/*from ww  w .j  a  va  2  s.c  o m*/
 *  Copyright 2009, 2011 Welocalize, Inc. 
 *  
 *  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.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**
     * Copy file to file
     * 
     * @param targetFile
     * @param file
     * @throws IOException
     */
    public static void copyFile(File targetFile, File file) throws IOException {

        InputStream is = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(targetFile);
        byte[] buffer = new byte[1024];
        int i = 0;
        while ((i = is.read(buffer)) != -1) {
            fos.write(buffer, 0, i);
        }
        is.close();
        fos.close();
    }

    /**
     * Copy file(InputStream) to file
     * 
     * @param targetFile
     * @param file
     * @throws IOException
     */
    public static void copyFile(File targetFile, InputStream file) throws IOException {

        FileOutputStream fos = new FileOutputStream(targetFile);
        byte[] buffer = new byte[1024];
        int i = 0;
        while ((i = file.read(buffer)) != -1) {
            fos.write(buffer, 0, i);
        }
        file.close();
        fos.close();
    }
}

Related

  1. copyFile(File srcFile, File dstFile)
  2. copyFile(File srcFile, File dstFile)
  3. copyFile(File srcPath, File dstPath)
  4. copyfile(File srFile, File dtFile)
  5. copyFile(File target, File source)
  6. copyFile(File targetFile, File file)
  7. copyFile(final File from, final File to)
  8. copyFile(final File fromFile, final File toFile)
  9. copyFile(final File fromFile, final File toFile)