集册 Java实例教程 将源文件复制到目标文件夹

将源文件复制到目标文件夹

欢马劈雪     最近更新时间:2020-01-02 10:19:05

535
将源文件复制到目标文件夹

/******************************************************************************

 * Copyright (c) 2002, 2004 IBM Corporation and others.

 * All rights reserved. This program and the accompanying materials

 * are made available under the terms of the Eclipse Public License v1.0

 * which accompanies this distribution, and is available at

 * http://www.eclipse.org/legal/epl-v10.html

 *

 * Contributors:

 *    IBM Corporation - initial API and implementation 

 ****************************************************************************/
 /*来自 
  时 代 J a v a 公 众 号 - nowjava.com*/

//package com.nowjava;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;


public class Main {

    /**

     * Copies a source file to a target folder

     * 

     * @param sourceFile

     *            the source file

     * @param targetFolder

     *            the target folder

     * @throws FileNotFoundException

     * @throws IOException

     */

    public static void copyFile(String sourceFile, String targetFolder)/**from n o w j a v a . c o m**/

            throws FileNotFoundException, IOException {

        assert (new File(sourceFile).isFile());

        assert (new File(targetFolder).isDirectory());


        File source = new File(sourceFile);

        copyFile(source.getParent(), targetFolder, source.getName());

    }


    /**

     * Copies a file in a source folder to a target folder

     * 

     * @param sourceFolder

     *            the source folder

     * @param targetFolder

     *            the target folder

     * @param name

     *            the file to copy

     * @throws FileNotFoundException

     * @throws IOException

     */

    private static void copyFile(String sourceFolder, String targetFolder,

            String name) throws FileNotFoundException, IOException {

        copyFile(sourceFolder, targetFolder, name, name);

    }


    /**

     * Copies a file in a source folder to a target folder

     * 

     * @param sourceFolder

     *            the source folder

     * @param targetFolder

     *            the target folder

     * @param sourceName

     *            of the source file to copy

     * @param targetName

     *            of the destination file to copy to

     * @throws FileNotFoundException

     * @throws IOException

     */

    public static void copyFile(String sourceFolder, String targetFolder,

            String sourceName, String targetName)

            throws FileNotFoundException, IOException {

        InputStream is = new FileInputStream(s
展开阅读全文