Here you can find the source of copyFile(File fromFile, File toFile)
Parameter | Description |
---|---|
fromFile | a parameter |
toFile | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void copyFile(File fromFile, File toFile) throws IOException
//package com.java2s; /*/*from w w w . j av a2 s . co m*/ * #%L * Osm2garminAPI * %% * Copyright (C) 2011 - 2012 Frantisek Mantlik <frantisek at mantlik.cz> * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 2 of the * License, or (at your option) any later version. * * 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, see * <http://www.gnu.org/licenses/gpl-2.0.html>. * #L% */ import java.io.*; public class Main { /** * * @param fromFile * @param toFile * @throws IOException */ public static void copyFile(File fromFile, File toFile) throws IOException { String fromFileName = fromFile.getName(); String toFileName = toFile.getName(); if (!fromFile.exists()) { throw new IOException("FileCopy: " + "no such source file: " + fromFile.getPath()); } if (!fromFile.isFile()) { throw new IOException("FileCopy: " + "can't copy directory: " + fromFileName); } if (!fromFile.canRead()) { throw new IOException("FileCopy: " + "source file is unreadable: " + fromFileName); } if (toFile.isDirectory()) { toFile = new File(toFile, fromFile.getName()); } String parent = toFile.getParent(); if (parent == null) { parent = System.getProperty("user.dir"); } File dir = new File(parent); if (!dir.exists()) { throw new IOException("FileCopy: " + "destination directory doesn't exist: " + parent); } if (dir.isFile()) { throw new IOException("FileCopy: " + "destination is not a directory: " + parent); } if (!dir.canWrite()) { throw new IOException("FileCopy: " + "destination directory is unwriteable: " + parent); } FileInputStream from; from = new FileInputStream(fromFile); copyFile(from, toFile); } /** * * @param from * @param toFile * @throws IOException */ public static void copyFile(InputStream from, File toFile) throws IOException { FileOutputStream to = null; try { to = new FileOutputStream(toFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = from.read(buffer)) != -1) { to.write(buffer, 0, bytesRead); // write } } finally { if (from != null) { try { from.close(); } catch (IOException e) { } } if (to != null) { try { to.close(); } catch (IOException e) { } } } } }