将文件从一个文件复制到另一个文件。
/** * e-Science Central * Copyright (C) 2008-2013 School of Computing Science, Newcastle University * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 2 as published by the Free Software Foundation at: * http://www.gnu.org/licenses/gpl-2.0.html * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA. *//*来 自 时代Java - nowjava.com*/ import java.io.*; import java.util.zip.*; import java.util.*; public class Main{ /** * Copy a file from one file to another. * This is a convenience method equivalent to calling * <code>{@link #copyFile(File, File) copyFile(source, new File(targetDir, source.getName())}</code>. */ public static void copyFileToDirectory(File source, File targetDir) throws IOException { copyFile(source, new File(targetDir, source.getName())); } /** Copy a file from one file to another */ public static void copyFile(File source, File target) throws IOException { if (source.exists()) { FileInputStream inStream = null; BufferedOutputStream outStream = null; try { inStream = new FileInputStream(source); outStream = new BufferedOutputStream(new FileOutputStream( /* n o w j a v a . c o m 提 供 */ target)); byte[] buffer = new byte[4096]; int len; while ((len = inStream.read(buffer)) > 0) { outStream.write(buffer, 0, len); } } finally { if (inStream != null) { try { inStream.close(); } catch (IOException ignore) { } } if (outStream != null) { try { outStream.close(); } catch (IOException ignore) { } } } } } /** Copy a file from one file to another with a listener */ public static void copyFile(File source, File target, InstrumentedInputStreamListener listener) throws IOException { if (source.exists()) { InstrumentedInputStream inStream = null; BufferedOutputStream outStream = null; try { inStream = new InstrumentedInputStream(new FileInputStream( source), listener); outStream = new BufferedOutputStream(new FileOutputStream( target)); byte[] buffer = new byte[4096]; int len;