Java BufferedInputStream Copy copyFile(File in, File out)

Here you can find the source of copyFile(File in, File out)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File in, File out) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    private static final int DEFAULT_BUFFER_SIZE = 16 * 1024;

    public static void copyFile(File in, File out) throws IOException {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {/*from   w  w  w . j  a v  a 2s .c  o  m*/
            bis = new BufferedInputStream(new FileInputStream(in));
            bos = new BufferedOutputStream(new FileOutputStream(out));
            int available = bis.available();
            available = available <= 0 ? DEFAULT_BUFFER_SIZE : available;
            int chunkSize = Math.min(DEFAULT_BUFFER_SIZE, available);
            byte[] buffer = new byte[chunkSize];
            int byteread = 0;
            while ((byteread = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, byteread);
            }
        } finally {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
        }
    }
}

Related

  1. copyFile(File from, File to)
  2. copyFile(File from, File to)
  3. copyFile(File fromFile, File toFile)
  4. copyFile(File fSrcFile, File fDestFile)
  5. copyFile(File in, File out)
  6. copyFile(File inFile, File outFile)
  7. copyFile(File inFile, File outFile)
  8. copyFile(File inputFile, File outputFile)
  9. copyFile(File origem, File destino)