Java FileInputStream Copy copyFile(String fromFile, String toFile)

Here you can find the source of copyFile(String fromFile, String toFile)

Description

Copies contents from one file to another

License

Open Source License

Parameter

Parameter Description
fromFile File from which to copy contents.
toFile File to which contents are written.

Declaration

public static void copyFile(String fromFile, String toFile) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w  ww. j a va  2s .c o  m*/
 * CDDL HEADER START
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://www.sun.com/cddl/cddl.html and legal/CDDLv1.0.txt
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at legal/CDDLv1.0.txt.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
 * CDDL HEADER END
 */

import java.io.BufferedOutputStream;
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 {
    /**
     * Copies contents from one file to another
     * @param fromFile File from which to copy contents.
     * @param toFile File to which contents are written.
     */
    public static void copyFile(String fromFile, String toFile) throws IOException {
        File from = new File(fromFile);
        File to = new File(toFile);

        FileInputStream inStream = new FileInputStream(from);

        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(to));
        copyInputStream(inStream, out);
    }

    /**
     * Copies contents from the InputStream to the OutputStream.
     * @param in InputStream from which to copy contents.
     * @param out OutputStream to which contents are written.
     */
    public static void copyInputStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[2048];
        int len;

        while ((len = in.read(buffer)) >= 0)
            out.write(buffer, 0, len);

        in.close();
        out.close();
    }
}

Related

  1. copyFile(String fileIn, String fileOut)
  2. copyFile(String fileInName, String fileOutName)
  3. copyFile(String fileName, String fromDir, String toDir)
  4. copyFile(String fileOutPut, String fileIn)
  5. copyFile(String from, String to)
  6. copyFile(String fromFilePath, String toFilePath)
  7. copyFile(String inFile, String outFile)
  8. copyFile(String inFileName, String outFileName)
  9. copyFile(String input, String output)