Here you can find the source of copyFile(String source, String destination)
Parameter | Description |
---|---|
source | source file path |
destination | destination location or file path |
File
instance
public static File copyFile(String source, String destination) throws IOException
//package com.java2s; /*//from ww w . java 2s. c o m * Copyright (c) 2007-2016 AREasy Runtime * * This library, AREasy Runtime and API for BMC Remedy AR System, is free software ("Licensed 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; * including but not limited to, the implied warranty of MERCHANTABILITY, NONINFRINGEMENT, * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ import java.io.*; public class Main { public static final int BUFFER_SIZE = 4096; /** * Copy resources from a source to a destination. * <p/> * If destination doesn't exists will be created. <br/> * If destination is directory will be appended source file name and will be created. * * @param source source file path * @param destination destination location or file path * @return final destination <Code>File</Code> instance */ public static File copyFile(String source, String destination) throws IOException { return copyFile(new File(source), new File(destination)); } /** * Copy resources from a source to a destination. * <p/> * If destination doesn't exists will be created. <br/> * If destination is directory will be appended source file name and will be created. * * @param sourceFile source file entity * @param destFile destination folder entity of real destination file entity * @return final destination <Code>File</Code> instance */ public static File copyFile(File sourceFile, File destFile) throws IOException { byte[] buffer = new byte[BUFFER_SIZE]; BufferedInputStream input; BufferedOutputStream output; //check if destination is directory. if (destFile.exists() && destFile.isDirectory()) { String destination = destFile.getPath() + File.separator + sourceFile.getName(); destFile = new File(destination); } //check if destination location doesn't exists. if (!destFile.getParentFile().exists()) destFile.getParentFile().mkdirs(); //check if destination file exists. if (!destFile.exists()) destFile.createNewFile(); input = new BufferedInputStream(new FileInputStream(sourceFile)); output = new BufferedOutputStream(new FileOutputStream(destFile)); int bytesRead; while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } input.close(); output.close(); return destFile; } }