Java FileInputStream Copy copyFile(String srcFilename, String dtsFilename)

Here you can find the source of copyFile(String srcFilename, String dtsFilename)

Description

pboon: Faster would be using FileChannel, but that has other problems.

License

Apache License

Declaration

public static void copyFile(String srcFilename, String dtsFilename) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2015 DANS - Data Archiving and Networked Services
 * /*  w  ww  .j a  v  a2s  .co m*/
 * 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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * pboon: Faster would be using FileChannel, but that has other problems.
     * When copying needs to be optimized, this is a place to look!
     */
    public static void copyFile(String srcFilename, String dtsFilename) throws FileNotFoundException, IOException {
        // maybe check if src != dst?
        InputStream in = null;
        OutputStream out = null;

        try {
            File f1 = new File(srcFilename);
            File f2 = new File(dtsFilename);
            in = new FileInputStream(f1);
            out = new FileOutputStream(f2);//Overwrite the file.

            byte[] buf = new byte[16 * 1024];// what is the optimal size?
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            //System.out.println("File copied.");
        } catch (FileNotFoundException e) {
            //System.out.println(e.getMessage());
            throw e;//new IOException("File not found");
        } catch (IOException e) {
            //System.out.println(e.getMessage());
            throw e;//new IOException("Could not copy files");
        } finally {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        }
    }
}

Related

  1. copyFile(String srcDir, String destDir)
  2. copyFile(String srcFile, String destFile)
  3. copyFile(String srcFile, String dstFile)
  4. copyFile(String srcFile, String dstFile)
  5. copyFile(String srcFileName, String destFileName)
  6. copyFile(String srcFilePath, String destFilePath)
  7. copyFile(String srcPath, String dstPath)
  8. copyFile(String srcPath, String dstPath, boolean replace)
  9. copyFile(String srFile, String dtFile)