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.*;

public class Main {
    public static void copyFile(File src, File dest) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);
        try {/*  w ww. ja va2  s .  c  o m*/
            writeInputToOutput(in, out, 8192);
        } finally {
            in.close();
            out.close();
        }
    }

    /** Reads bytes from an InputStream and transfers them to an OutputStream.
     * It will block and keep reading until the InputStream is exhausted and returns a -1 on read().
     * It will flush() the OutputStream, but it does NOT close the InputStream or OutputStream.
     * @param bufferSize The size of the chunks that will be used for the transfer. Large buffers take more memory, but are generally more efficient. Use IOUtils.CHUNK_SIZE when in doubt.
     * @param in The stream to read from.
     * @param out The stream to write to.
     * @return The actual number of bytes transferred.
     * @throws java.io.IOException
     */
    static public long writeInputToOutput(InputStream in, OutputStream out, int bufferSize) throws IOException {
        if (in == null)
            throw new IllegalArgumentException("You cannot pass a null InputStream");
        if (out == null)
            throw new IllegalArgumentException("You cannot pass a null OutputStream");
        byte[] buffer = new byte[bufferSize];
        int lastBytesRead;
        long totalBytesRead = 0;
        while ((lastBytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, lastBytesRead);
            totalBytesRead += lastBytesRead;
        }
        out.flush();
        return totalBytesRead;
    }

    /**
     * Reads an InputStream and writes it to an OutputStream. Stops after encountering stopSequence. This reads and writes a single byte at a time, so it is a good idea for performance purposes to use buffered streams.
     * @param in The InputStream to read. Must be non-null.
     * @param out the OutputStream to write to. If null, then the input will still be scanned, but nothing will be written.
     * @param stopSequence Reading will stop after encountering this byte sequence.
     * @return The total number of bytes read.
     * @throws IOException
     */
    static public long writeInputToOutput(InputStream in, OutputStream out, byte[] stopSequence)
            throws IOException {
        int result = 0;
        int sequenceMark = 0;
        int sequenceLength = stopSequence.length;
        int eachByte;
        while ((sequenceMark < sequenceLength) && (result < 1600)) { //FIX! Temporary stopgap to prevent crash; stop after reading 1600 bytes
            eachByte = in.read();
            if (eachByte == -1)
                break;
            //System.out.print( (char)eachByte );
            out.write(eachByte);
            if (eachByte == stopSequence[sequenceMark])
                sequenceMark++;
            else
                sequenceMark = 0;
            result++;
        }
        return result;
    }
}

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)
  7. copyFile(File src, File dest)
  8. copyFile(File src, File dest, int bufSize)
  9. copyFile(File src, File dst)