Here you can find the source of copyFile(File source, File target)
Parameter | Description |
---|---|
source | a parameter |
target | a parameter |
Parameter | Description |
---|---|
IOException | Is thrown, if the source file does not exist |
public static void copyFile(File source, File target) throws IOException
//package com.java2s; /** /*w w w .j a va 2s . c o m*/ * Copyright (c) 2009-2011, The HATS Consortium. All rights reserved. * This file is licensed under the terms of the Modified BSD License. */ import java.io.*; public class Main { /** * Copies a file to a different location * @param source * @param target * @throws IOException Is thrown, if the source file does not exist */ public static void copyFile(File source, File target) throws IOException { InputStream inputStream = new FileInputStream(source); OutputStream outputStream = new FileOutputStream(target); try { copyFile(inputStream, outputStream); } finally { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } } } /** * Copies an input stream into an output stream * @param inputStream * @param outputStream * @throws IOException */ public static void copyFile(InputStream inputStream, OutputStream outputStream) throws IOException { int length; byte[] buffer = new byte[4096]; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } } }