/* * Copyright 2012 jMethods, 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. */ package com.myjavaworld.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * @author Sai Pullabhotla */ public class FileUtilities { public static void copyFile(File source, File target) throws IOException { BufferedInputStream in = null; BufferedOutputStream out = null; final int bufferSize = 4096; byte[] buffer = new byte[bufferSize]; int bytesRead = 0; try { in = new BufferedInputStream(new FileInputStream(source), bufferSize); out = new BufferedOutputStream(new FileOutputStream(target), bufferSize); while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }