Java FileInputStream Copy copyFile(String srcFile, String dstFile)

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

Description

copy File

License

Apache License

Declaration

public static final void copyFile(String srcFile, String dstFile) throws IOException 

Method Source Code


//package com.java2s;
/* org.agiso.core.lang.util.FileUtils (11-02-2014)
 * //from  w  ww .  j ava2  s.c o  m
 * FileUtils.java
 * 
 * Copyright 2014 agiso.org
 * 
 * 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.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static final void copyFile(String srcFile, String dstFile) throws IOException {
        copyFile(new File(srcFile), new File(dstFile));
    }

    public static final void copyFile(File srcFile, File dstFile) throws IOException {
        InputStream is = null;
        try {
            is = new FileInputStream(srcFile);

            copyFile(is, dstFile);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static final void copyFile(InputStream is, File dstFile) throws IOException {
        OutputStream os = null;
        try {
            os = new FileOutputStream(dstFile);

            int len;
            byte[] buf = new byte[1024];
            while ((len = is.read(buf)) > 0) {
                os.write(buf, 0, len);
            }
        } finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

Related

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