Here you can find the source of copyFile(File fromFile, File toFile)
Parameter | Description |
---|---|
fromFile | a parameter |
toFile | a parameter |
Parameter | Description |
---|---|
FileNotFoundException | an exception |
IOException | an exception |
public static void copyFile(File fromFile, File toFile) throws FileNotFoundException, IOException
//package com.java2s; /*/*from ww w .j a v a2 s . c o m*/ * Copyright 2012 James Moger * * 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.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Main { /** * Copies a file to another file. * * @param fromFile * @param toFile * @throws FileNotFoundException * @throws IOException */ public static void copyFile(File fromFile, File toFile) throws FileNotFoundException, IOException { toFile.getParentFile().mkdirs(); BufferedInputStream bufin = null; FileOutputStream fos = null; try { bufin = new BufferedInputStream(new FileInputStream(fromFile)); fos = new FileOutputStream(toFile); int len = 8196; byte[] buff = new byte[len]; int n = 0; while ((n = bufin.read(buff, 0, len)) != -1) { fos.write(buff, 0, n); } } finally { try { bufin.close(); } catch (Throwable t) { } try { fos.close(); } catch (Throwable t) { } } toFile.setLastModified(fromFile.lastModified()); } }