ikrs.io.fileio
Class FileCopy

java.lang.Object
  extended by ikrs.io.fileio.FileCopy

public class FileCopy
extends java.lang.Object

The standard java libraries have no file- and directory- copy routines. This class is a simple solution.


Field Summary
static int DEFAULT_BUFFER_SIZE
          The default buffer size to use (in bytes).
 
Constructor Summary
FileCopy()
           
 
Method Summary
static boolean copy(java.io.File source, java.io.File destination)
          Copy the data from source to destination.
static void main(java.lang.String[] argv)
          For testing purposes only.
static long transfer(java.io.File sourceFile, java.io.File destinationFile, int bufferSize)
          Transfer all bytes from the given input file into the destination output file (non of the files must be a directory).
static long transfer(java.io.InputStream source, java.io.OutputStream destination, int bufferSize)
          Transfer all bytes from the given input stream into the destination output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The default buffer size to use (in bytes).

See Also:
Constant Field Values
Constructor Detail

FileCopy

public FileCopy()
Method Detail

copy

public static boolean copy(java.io.File source,
                           java.io.File destination)
                    throws java.lang.NullPointerException,
                           java.io.IOException
Copy the data from source to destination. Source and destination MUST be regular files or directories. There are four possible cases. Example: (i) source is a regular file, destination is a regular file. Then the bytes from source will be written into the destination. If the destination already exists it will be overwritten. (ii) source is a directory, destination is a regular file. This will cause an IllegalArgumentException to be thrown! (iii) source is a regular file, destination is a directory. A new file will be created inside the destination directory having the same name as source. (iv) source is a directory, destination is a directory. The destination directory will be created (if not exists) and all files and sub directories from source will be copied into the destination directory recursively.

Throws:
java.lang.NullPointerException
java.io.IOException

transfer

public static long transfer(java.io.File sourceFile,
                            java.io.File destinationFile,
                            int bufferSize)
                     throws java.lang.NullPointerException,
                            java.lang.IllegalArgumentException,
                            java.io.IOException
Transfer all bytes from the given input file into the destination output file (non of the files must be a directory). The method returns the number of bytes that were actually transfered during the progress.

Parameters:
sourceFile - The input file to read from.
destinationFile - The output file to write to.
bufferSize - The buffer size to use (a value from 512 to 2048 should be okay for most purposes).
Throws:
java.lang.NullPointerException - If sourceFile or destinationFile is null.
java.lang.IllegalArgumentException - If the buffer size is not in range (must be > 0).
java.io.IOException - If any IO errors occur.

transfer

public static long transfer(java.io.InputStream source,
                            java.io.OutputStream destination,
                            int bufferSize)
                     throws java.lang.NullPointerException,
                            java.lang.IllegalArgumentException,
                            java.io.IOException
Transfer all bytes from the given input stream into the destination output stream. The method returns the number of bytes that were actually transfered during the progress.

Parameters:
source - The input stream to read from.
destination - The output stream to write to.
bufferSize - The buffer size to use (a value from 512 to 2048 should be okay for most purposes).
Throws:
java.lang.NullPointerException - If source or destination is null.
java.lang.IllegalArgumentException - If the buffer size is not in range (must be > 0).
java.io.IOException - If any IO errors occur.

main

public static void main(java.lang.String[] argv)
For testing purposes only.