Here you can find the source of copyFile(File sourceFile, File destFile)
public static void copyFile(File sourceFile, File destFile)
//package com.java2s; /*//from w w w . j a va 2 s. co m * Copyright 2015 Red Hat, Inc. and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * * 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.nio.channels.FileChannel; public class Main { public static void copyFile(File sourceFile, File destFile) { destFile.getParentFile().mkdirs(); if (!destFile.exists()) { try { destFile.createNewFile(); } catch (IOException ioe) { throw new RuntimeException("Unable to create file " + destFile.getAbsolutePath(), ioe); } } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } catch (IOException ioe) { throw new RuntimeException( "Unable to copy " + sourceFile.getAbsolutePath() + " to " + destFile.getAbsolutePath(), ioe); } finally { if (source != null) { try { source.close(); } catch (IOException e) { } } if (destination != null) { try { destination.close(); } catch (IOException e) { } } } } }