Here you can find the source of copyFile(File srcFile, File destFile)
Parameter | Description |
---|---|
srcFile | The file to copy |
destFile | The file where srcFile is copied |
public static void copyFile(File srcFile, File destFile) throws IOException
//package com.java2s; /* /*from w w w . j av a 2s . c o m*/ * Copyright (C) 2016 Bruce Beisel * * 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 3 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/>. */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Main { /** * Copy a file to another * * @param srcFile The file to copy * @param destFile The file where srcFile is copied * * @exception IOException Thrown if either the source cannot be read or the destination cannot be written */ public static void copyFile(File srcFile, File destFile) throws IOException { FileOutputStream os = null; try (FileInputStream is = new FileInputStream(srcFile)) { os = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; int bcount; while ((bcount = is.read(buffer)) > 0) os.write(buffer, 0, bcount); } finally { if (os != null) os.close(); } } }