Here you can find the source of copyFile(File inFile, File outFile)
Parameter | Description |
---|---|
inFile | source file |
outFile | destination file |
Parameter | Description |
---|---|
IOException | if anything bad happens. |
public static void copyFile(File inFile, File outFile) throws IOException
//package com.java2s; /*// ww w.j a v a 2s. c o m * org.daisy.util (C) 2005-2008 Daisy Consortium * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library 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 Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class Main { /** * Copy a file using java.nio * * @param inFile * source file * @param outFile * destination file * @throws IOException * if anything bad happens. */ public static void copyFile(File inFile, File outFile) throws IOException { copyFile(inFile, outFile, false); } /** * Copy a file using java.nio * * @param inFile * source file * @param outFile * destination file * @param keepLastModified * keep the "last modified" date from the input file * @throws IOException * if anything bad happens. */ public static void copyFile(File inFile, File outFile, boolean keepLastModified) throws IOException { if (inFile.equals(outFile)) return; if (!inFile.isFile()) throw new IOException(inFile.getAbsolutePath() + " is not a file"); if (outFile.exists()) { if (!outFile.isFile()) { throw new IOException(outFile.getAbsolutePath() + " is not a file"); } } else { createDirectory(outFile.getParentFile()); outFile.createNewFile(); } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(inFile).getChannel(); destination = new FileOutputStream(outFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } if (keepLastModified) { outFile.setLastModified(inFile.lastModified()); } } /** * Make sure a directory exists. * @param dir * @throws IOException * if the dir could not be created or if a regular file with the * specified name exists. */ public static File createDirectory(File dir) throws IOException { if (!dir.exists()) { boolean result = dir.mkdirs(); if (!result) { throw new IOException("Could not create directory " + dir); } } else if (!dir.isDirectory()) { throw new IOException(dir + " already exists and is not a directory"); } return dir; } }