Here you can find the source of copyFile(String src, File dest)
Parameter | Description |
---|---|
src | The path for the source file. |
dest | The destination file or directory. |
public static String copyFile(String src, File dest) throws IOException
//package com.java2s; /*/* w w w . j a v a 2 s .co m*/ * The contents of this file are subject to the Mozilla Public License * Version 1.1 (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.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. * * The Original Code is the Kowari Metadata Store. * * The Initial Developer of the Original Code is Plugged In Software Pty * Ltd (http://www.pisoftware.com, mailto:info@pisoftware.com). Portions * created by Plugged In Software Pty Ltd are Copyright (C) 2001,2002 * Plugged In Software Pty Ltd. All Rights Reserved. * * Contributor(s): N/A. * * [NOTE: The text of this Exhibit A may differ slightly from the text * of the notices in the Source Code files of the Original Code. You * should use the text of this Exhibit A rather than the text found in the * Original Code Source Code for Your Modifications.] * */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** The size of the buffer used to read and write to files. 0.25MB. */ private static final int BUFFER_SIZE = 1024 * 256; /** * @see #copyFile(File, File) * @param src The path for the source file. * @param dest The destination file or directory. */ public static String copyFile(String src, File dest) throws IOException { return copyFile(new File(src), dest); } /** * @see #copyFile(File, File) * @param src The source file. * @param dest The path for the destination file or directory. */ public static String copyFile(File src, String dest) throws IOException { return copyFile(src, new File(dest)); } /** * @see #copyFile(File, File) * @param src The path for the source file. * @param dest The path for the destination file or directory. */ public static String copyFile(String src, String dest) throws IOException { return copyFile(new File(src), new File(dest)); } /** * Copies a file from one place to another. This is similar to the "cp" utility. * @param src The source file. * @param dest The destination file or directory. If this specifies a directory, * then the filename part of <value>src</value> will be used. * @return The path of the file that was written. * @throws IOException If there was a problem reading or writing the file. */ public static String copyFile(File src, File dest) throws IOException { if (dest.isDirectory()) dest = new File(dest, src.getName()); InputStream in = null; OutputStream out = null; try { in = new FileInputStream(src); out = new FileOutputStream(dest); byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = in.read(buffer)) >= 0) { if (len != 0) out.write(buffer, 0, len); } } finally { if (in != null) in.close(); if (out != null) out.close(); } return dest.getPath(); } }