Java FileInputStream Copy copyFile(String srcFile, String destFile)

Here you can find the source of copyFile(String srcFile, String destFile)

Description

copy File

License

Open Source License

Declaration

public static File copyFile(String srcFile, String destFile) throws SecurityException, IOException 

Method Source Code


//package com.java2s;
/*/*from w  w w.j  av a  2 s . c o  m*/
 * $Id$
 *
 * Copyright (C) 2003-2015 JNode.org
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; If not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static File copyFile(String srcFile, String destFile) throws SecurityException, IOException {
        return copyInputStreamToFile(new FileInputStream(new File(srcFile)), destFile);
    }

    public static File copyInputStreamToFile(InputStream src, String destFile)
            throws SecurityException, IOException {
        File dest = new File(destFile);

        if (dest.exists())
            dest.delete();

        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];
            int read = -1;
            do {
                read = src.read(buffer, 0, buffer.length);
                if (read > 0) {
                    fos.write(buffer, 0, read);
                }
            } while (read != -1);
        } finally {
            if (src != null)
                src.close();

            if (fos != null) {
                fos.flush();
                fos.close();
            }
        }

        return dest;
    }
}

Related

  1. copyFile(String src, String dst)
  2. copyFile(String src, String dst)
  3. copyFile(String src, String tar)
  4. copyFile(String src, String to)
  5. copyFile(String srcDir, String destDir)
  6. copyFile(String srcFile, String dstFile)
  7. copyFile(String srcFile, String dstFile)
  8. copyFile(String srcFileName, String destFileName)
  9. copyFile(String srcFilename, String dtsFilename)