Java BufferedInputStream Copy copyFile(String fromFile, String toFile)

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

Description

Copy a file.

License

Apache License

Parameter

Parameter Description
fromFile the source file
toFile the destination file

Exception

Parameter Description
Exception a generic exception

Declaration

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

Method Source Code


//package com.java2s;
/*/*from   ww  w.  j a  v a2 s  .com*/
 ===========================================================================
   Copyright 2002-2010 Martin Dvorak
    
   Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 ===========================================================================
*/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Main {
    /**
     * Copy a file.
     * 
     * @param fromFile
     *            the source file
     * @param toFile
     *            the destination file
     * @throws Exception
     *             a generic exception
     */
    public static void copyFile(String fromFile, String toFile) throws Exception {
        File from = new File(fromFile);
        File to = new File(toFile);
        to.createNewFile();
        FileInputStream fileInputStream = new FileInputStream(from);
        BufferedInputStream in = new BufferedInputStream(fileInputStream);

        FileOutputStream fileOutputStream = new FileOutputStream(to);
        BufferedOutputStream out = new BufferedOutputStream(fileOutputStream);

        byte[] buffer = new byte[1024];
        int i;
        while (in.available() > 0) {
            i = in.read(buffer);
            out.write(buffer, 0, i);
        }
        out.flush();
        in.close();
        out.close();
    }
}

Related

  1. copyFile(InputStream src, File dest)
  2. copyFile(InputStream src, File dst)
  3. copyFile(String dest_path, String src_path)
  4. copyFile(String fileName, File sourceRoot, File targetRoot, Set copied)
  5. copyFile(String from, String to)
  6. copyFile(String source, String dest)
  7. copyFile(String source, String destination)
  8. copyFile(String source, String destination)
  9. copyFile(String sourceFilePath, String destinationFilePath)