Java FileInputStream Copy copyFile(String fileIn, String fileOut)

Here you can find the source of copyFile(String fileIn, String fileOut)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(String fileIn, String fileOut) throws IOException 

Method Source Code

//package com.java2s;
/*/* www. java  2  s.c  o  m*/
 * Copyright (C) 2009, Edmundo Albuquerque de Souza e Silva.
 *
 * This file may be distributed under the terms of the Q Public License
 * as defined by Trolltech AS of Norway and appearing in the file
 * LICENSE.QPL included in the packaging of this file.
 *
 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

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 {
    private static final int BUFFER_SIZE = 2048;

    public static void copyFile(String fileIn, String fileOut) throws IOException {
        copyFile(new File(fileIn), new File(fileOut));
    }

    public static void copyFile(File fileIn, File fileOut) throws IOException {
        InputStream is = new FileInputStream(fileIn);
        OutputStream os = new FileOutputStream(fileOut);
        copyStream(is, os);
        is.close();
        os.close();
    }

    public static void copyStream(InputStream is, OutputStream os) throws IOException {
        byte[] buf = new byte[BUFFER_SIZE];
        while (true) {
            int len = is.read(buf);
            if (len == -1)
                return;
            os.write(buf, 0, len);
        }
    }
}

Related

  1. copyFile(InputStream in, File dst)
  2. copyFile(InputStream in, File to)
  3. copyFile(InputStream in, String destFile)
  4. copyFile(OutputStream out, InputStream in)
  5. copyFile(String f1, String f2)
  6. copyFile(String fileInName, String fileOutName)
  7. copyFile(String fileName, String fromDir, String toDir)
  8. copyFile(String fileOutPut, String fileIn)
  9. copyFile(String from, String to)