Java BufferedInputStream Copy copyFile(File fromFile, File toFile)

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

Description

Copies a file to another file.

License

Apache License

Parameter

Parameter Description
fromFile a parameter
toFile a parameter

Exception

Parameter Description
FileNotFoundException an exception
IOException an exception

Declaration

public static void copyFile(File fromFile, File toFile) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*/*from  ww  w  .j  a  v a2 s .  c  o  m*/
 * Copyright 2012 James Moger
 *
 * 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.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**
     * Copies a file to another file.
     * 
     * @param fromFile
     * @param toFile
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static void copyFile(File fromFile, File toFile) throws FileNotFoundException, IOException {
        toFile.getParentFile().mkdirs();
        BufferedInputStream bufin = null;
        FileOutputStream fos = null;
        try {
            bufin = new BufferedInputStream(new FileInputStream(fromFile));
            fos = new FileOutputStream(toFile);
            int len = 8196;
            byte[] buff = new byte[len];
            int n = 0;
            while ((n = bufin.read(buff, 0, len)) != -1) {
                fos.write(buff, 0, n);
            }
        } finally {
            try {
                bufin.close();
            } catch (Throwable t) {
            }
            try {
                fos.close();
            } catch (Throwable t) {
            }
        }
        toFile.setLastModified(fromFile.lastModified());
    }
}

Related

  1. copyFile(File file, File ziel)
  2. copyFile(File from, File to)
  3. copyFile(File from, File to)
  4. copyFile(File from, File to)
  5. copyFile(File from, File to)
  6. copyFile(File fSrcFile, File fDestFile)
  7. copyFile(File in, File out)
  8. copyFile(File in, File out)
  9. copyFile(File inFile, File outFile)