Here you can find the source of copyFile(String srcFilename, String dtsFilename)
public static void copyFile(String srcFilename, String dtsFilename) throws FileNotFoundException, IOException
//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(); } } }