Java FileInputStream Copy copyFile(File srcFile, File destFile)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File srcFile, File destFile) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source 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;

import java.util.zip.CRC32;

public class Main {
    private static final String JPEG = "jpeg";
    private static final String JPG = "jpg";
    private static final int BUFFER_SIZE = 4 * 1024;
    private static final boolean CLOCK = true;
    private static final boolean VERIFY = true;

    public static void copyFile(File srcFile, File destFile) throws IOException {
        if (!srcFile.getPath().toLowerCase().endsWith(JPG) && !srcFile.getPath().toLowerCase().endsWith(JPEG)) {
            return;
        }/*from  ww w  .  jav  a2  s.  c  om*/
        final InputStream in = new FileInputStream(srcFile);
        final OutputStream out = new FileOutputStream(destFile);
        try {
            long millis = System.currentTimeMillis();
            CRC32 checksum;
            if (VERIFY) {
                checksum = new CRC32();
                checksum.reset();
            }
            final byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = in.read(buffer);
            while (bytesRead >= 0) {
                if (VERIFY) {
                    checksum.update(buffer, 0, bytesRead);
                }
                out.write(buffer, 0, bytesRead);
                bytesRead = in.read(buffer);
            }
            if (CLOCK) {
                millis = System.currentTimeMillis() - millis;
            }
        } catch (IOException e) {
            throw e;
        } finally {
            out.close();
            in.close();
        }
    }
}

Related

  1. copyFile(File srcFile, File destFile)
  2. copyFile(File srcFile, File destFile)
  3. copyFile(File srcFile, File destFile)
  4. CopyFile(File srcFile, File destFile)
  5. copyFile(File srcFile, File destFile)
  6. copyFile(File srcFile, File destFile)
  7. copyFile(File srcFile, File destFile)
  8. copyFile(File srcFile, File destFile)
  9. copyFile(File srcFile, File destFile, boolean recursive)