Java FileInputStream Copy copyFile(File destFile, File srcFile)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File destFile, File srcFile) 

Method Source Code


//package com.java2s;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {

    public static void copyFile(File destFile, File srcFile) {
        if (!srcFile.exists() || null == destFile) {
            return;
        }/*from w w w  . j  a v  a  2 s.c o  m*/
        if (null != destFile.getParentFile()) {
            destFile.getParentFile().mkdirs();
        }
        FileInputStream fis = null;
        FileOutputStream fos = null;
        int buffReaded = 0;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            byte[] buff = new byte[1024];
            while ((buffReaded = fis.read(buff)) != -1) {
                fos.write(buff, 0, buffReaded);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(fis);
            close(fos);
        }
    }

    public static void close(Closeable c) {
        if (null != c) {
            try {
                c.close();
                c = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. copyFile(File aSource, File aDest)
  2. copyFile(File base, String relfilepath, File targetdir)
  3. copyFile(File copyFrom, File copyTo)
  4. copyFile(File copyFrom, File copyTo)
  5. copyFile(File destfile, File srcfile)
  6. copyFile(File destination, File source)
  7. copyFile(File existingFile, File destFile)
  8. copyFile(File file, File destPath)
  9. copyFile(File file, File dir)