Here you can find the source of copyFile(File from, File to)
Parameter | Description |
---|---|
from | the file to be copied. |
to | the destination file. |
public static boolean copyFile(File from, File to)
/*/*from w w w. ja v a2 s. c om*/ Copyright 2008 Flaptor (flaptor.com) 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.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.zip.CRC32; import java.util.zip.Checksum; import org.apache.log4j.Logger; public class Main{ private static Logger logger = Logger.getLogger(Execute.whoAmI()); /** * Copies one file to another, optionally appending. * @param from the file to be copied. * @param to the destination file. * @return true if successful, false otherwise. */ public static boolean copyFile(File from, File to, boolean append) { boolean ok = true; try { BufferedInputStream in = new BufferedInputStream( new FileInputStream(from), 64 * 1024); BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(to, append), 64 * 1024); byte[] buf = new byte[8 * 1024]; int len = 0; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } in.close(); out.close(); } catch (IOException e) { logger.warn("Copying file " + from.getName() + " to " + to.getName() + ": " + e); ok = false; } return ok; } /** * Copies one file to another, overwriting the destination file. * @param from the file to be copied. * @param to the destination file. * @return true if successful, false otherwise. */ public static boolean copyFile(File from, File to) { return copyFile(from, to, false); } /** * Copies one file to another * @param from the name of the file to be copied. * @param to the name of the destination file. * @return true if successful, false otherwise. */ public static boolean copyFile(String from, String to) { return copyFile(new File(from), new File(to)); } }