Java FileInputStream Copy copyFile(File src, File dest)

Here you can find the source of copyFile(File src, File dest)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File src, File dest) 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;

public class Main {
    public static void copyFile(File src, File dest) throws IOException {
        FileInputStream fIn = null;
        FileOutputStream fOut = null;

        try {//from ww  w .  j  a va 2 s  .c om
            fIn = new FileInputStream(src);
            fOut = new FileOutputStream(dest);
            copy(fIn, fOut);
        } finally {
            if (fIn != null)
                fIn.close();
            if (fOut != null)
                fOut.close();
        }
    }

    public static int copy(InputStream in, OutputStream out) throws IOException {
        int totalCount = 0;
        byte[] buf = new byte[4096];
        int count = 0;

        while (count >= 0) {
            count = in.read(buf);
            totalCount += count;

            if (count > 0) {
                out.write(buf, 0, count);
            }
        }

        return totalCount;
    }

    public static int copy(InputStream in, OutputStream out, int contentSize) throws IOException {
        int totalCount = 0;
        byte[] buf = new byte[4096];
        int bytesIn = 0;

        /*Timer timer = new Timer(180); // 3 minute timeout
            
        while (bytesIn >= 0 && totalCount < contentSize) {
        if (in.available() > 0) {
                bytesIn = in.read(buf);
                totalCount += bytesIn;
            
                if (bytesIn > 0) {
                        out.write(buf, 0, bytesIn);
                }
                timer.reset();
        }
        if (timer.isTimedOut()) {
                throw new IOException("Copy time-out after " + Integer.toString(totalCount) + " bytes");
        }
        }*/
        while ((bytesIn >= 0) && (totalCount < contentSize)) {
            bytesIn = in.read(buf);
            totalCount += bytesIn;

            if (bytesIn > 0) {
                out.write(buf, 0, bytesIn);
            }
        }

        return totalCount;
    }
}

Related

  1. copyFile(File src, File dest)
  2. copyFile(File src, File dest)
  3. copyFile(File src, File dest)
  4. copyFile(File src, File dest)
  5. copyFile(File src, File dest)
  6. copyFile(File src, File dest, int bufSize)
  7. copyFile(File src, File dst)
  8. copyFile(File src, File dst)
  9. copyFile(File src, File dst)