Copies a source file to a target folder - Java File Path IO

Java examples for File Path IO:File Copy

Description

Copies a source file to a target folder

Demo Code

/******************************************************************************
 * 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:/* ww w  .ja  v a 2s . co m*/
 *    IBM Corporation - initial API and implementation 
 ****************************************************************************/
//package com.java2s;
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)
            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(sourceFolder + File.separator
                + sourceName);
        OutputStream os = new FileOutputStream(targetFolder
                + File.separator + targetName);
        byte[] buffer = new byte[102400];
        while (true) {
            int len = is.read(buffer);
            if (len < 0)
                break;
            os.write(buffer, 0, len);
        }
        is.close();
        os.close();
    }
}

Related Tutorials