org.dmonix.io
Class IOUtil

java.lang.Object
  extended byorg.dmonix.io.IOUtil

public abstract class IOUtil
extends java.lang.Object

I/O related utility methods.

Copyright: Copyright (c) 2003

Company: dmonix.org

Version:
1.0
Author:
Peter Nerg

Method Summary
static void copyFile(java.io.File source, java.io.File target)
          Copy a file from an other file.
static void copyFile(java.io.InputStream source, java.io.File target)
          Copy a file from a source.
static void copyFile(java.net.URL url, java.io.File target)
          Copy a file from a URL.
static void copyFileNoException(java.io.File source, java.io.File target)
          Copy a file from a source.
static void copyFileNoException(java.io.InputStream source, java.io.File target)
          Copy a file from a source.
static void copyFileNoException(java.net.URL url, java.io.File target)
          Copy a file from a URL.
static void copyStreams(java.io.InputStream source, java.io.OutputStream target)
          Copy data between two streams.
static void copyStreams(java.io.InputStream source, java.io.OutputStream target, boolean closeSource, boolean closeTarget)
          Copy data between two streams.
static int deleteDirectory(java.io.File dir)
          The method deletes a directory.
static boolean deleteFile(java.io.File file)
          Remove a single file.
static boolean deleteFile(java.lang.String file)
          Remove a single file.
static void deleteFiles(java.io.File[] files)
          Remove an array of files.
static int deleteFiles(java.io.File dir, java.io.FileFilter filter)
          Recursively deletes all files in the provided path that match the filter.
static int deleteFiles(java.io.File dir, java.io.FilenameFilter filter)
          Recursively deletes all files in the provided path that match the filter.
static java.lang.String getRelativePath(java.io.File directory, java.io.File file)
          Return the relative path between the directory and the file.
static java.io.File[] listFiles(java.io.File dir)
          Recursively lists all files for a directory.
static java.io.File[] listFiles(java.io.File dir, java.io.FileFilter filter)
          Recursively lists all files for a directory.
static java.io.File[] listFiles(java.io.File dir, java.io.FilenameFilter filter)
          Recursively lists all files for a directory.
static void makeBackup(java.io.File file)
          Create a backup file of the specified file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

copyFile

public static void copyFile(java.io.InputStream source,
                            java.io.File target)
                     throws java.io.IOException
Copy a file from a source.

Parameters:
target - The target file
source - The source
Throws:
java.io.IOException

copyFile

public static void copyFile(java.io.File source,
                            java.io.File target)
                     throws java.io.IOException
Copy a file from an other file.

Parameters:
source - The source file
target - The target file
Throws:
java.io.IOException

copyFile

public static void copyFile(java.net.URL url,
                            java.io.File target)
                     throws java.io.IOException
Copy a file from a URL.

Parameters:
url - The source URL
target - The target file
Throws:
java.io.IOException

copyFileNoException

public static void copyFileNoException(java.io.File source,
                                       java.io.File target)
Copy a file from a source.
Any exceptions are ignored and logged.

Parameters:
target - The target file
source - The source

copyFileNoException

public static void copyFileNoException(java.io.InputStream source,
                                       java.io.File target)
Copy a file from a source.
Any exceptions are ignored and logged.

Parameters:
target - The target file
source - The source

copyFileNoException

public static void copyFileNoException(java.net.URL url,
                                       java.io.File target)
Copy a file from a URL.
Any exceptions are ignored and logged.

Parameters:
target - The target file
url - The source URL

copyStreams

public static void copyStreams(java.io.InputStream source,
                               java.io.OutputStream target)
                        throws java.io.IOException
Copy data between two streams.
This is the same as a call to copyStreams(InputStream, OutputStream, true, true)

Parameters:
source - The source stream
target - The target stream
Throws:
java.io.IOException

copyStreams

public static void copyStreams(java.io.InputStream source,
                               java.io.OutputStream target,
                               boolean closeSource,
                               boolean closeTarget)
                        throws java.io.IOException
Copy data between two streams.

Parameters:
source - The source stream
target - The target stream
closeSource - If the source stream is to be closed
closeTarget - If the target stream is to be closed
Throws:
java.io.IOException

deleteDirectory

public static int deleteDirectory(java.io.File dir)
The method deletes a directory.
Since File.delete() only works on empty directories this method will recursively find and delete all sub-directories and files to the input directory.
If the input is a non-directory file it will simply be deleted.

Parameters:
dir - The directory to be deleted
Returns:
The number of deleted files/directories

deleteFile

public static boolean deleteFile(java.io.File file)
Remove a single file.
Note that if the file is a non-empty directory it cannot be deleted.
In order to delete a non-empty directory use deleteDirectory(File)
The delete result will be logged using Level.FINE

Parameters:
file - The file to remove
Returns:
The result of the operation
See Also:
deleteDirectory(File)

deleteFile

public static boolean deleteFile(java.lang.String file)
Remove a single file.
Note that if the file is a non-empty directory it cannot be deleted.
In order to delete a non-empty directory use deleteDirectory(File)
The delete result will be logged using Level.FINE

Parameters:
file - The file to remove
Returns:
The result of the operation
See Also:
deleteDirectory(File)

deleteFiles

public static int deleteFiles(java.io.File dir,
                              java.io.FileFilter filter)
Recursively deletes all files in the provided path that match the filter.
Each delete operation invokes deleteFile(File)

Parameters:
dir - The directory path
filter - The filter
Returns:
The number of deleted files

deleteFiles

public static int deleteFiles(java.io.File dir,
                              java.io.FilenameFilter filter)
Recursively deletes all files in the provided path that match the filter.
Each delete operation invokes deleteFile(File)

Parameters:
dir - The directory path
filter - The filter
Returns:
The number of deleted files

deleteFiles

public static void deleteFiles(java.io.File[] files)
Remove an array of files.

Parameters:
files - The files to remove

getRelativePath

public static java.lang.String getRelativePath(java.io.File directory,
                                               java.io.File file)
Return the relative path between the directory and the file.
The method returns null if: E.g. the directory /etc/foo/ and the file /etc/foo/data/file.txt results in data/file.txt/

Parameters:
directory - The directory
file - The file
Returns:
The relative path

listFiles

public static java.io.File[] listFiles(java.io.File dir)
Recursively lists all files for a directory.
If the input is a non-directory file this file will be returned in a single item File array.
Invoking this method is equal to:
listFiles(File, AcceptAllFileFilter)

Parameters:
dir - The directory to list items for
Returns:
A File array with all files and directories.
See Also:
AcceptAllFilter

listFiles

public static java.io.File[] listFiles(java.io.File dir,
                                       java.io.FileFilter filter)
Recursively lists all files for a directory.
If the input is a non-directory file this file will be returned in a single item File array.
Only items matching the filter will be returned

Parameters:
dir - The directory to list items for
filter - The filter
Returns:
A File array with all files and directories.

listFiles

public static java.io.File[] listFiles(java.io.File dir,
                                       java.io.FilenameFilter filter)
Recursively lists all files for a directory.
If the input is a non-directory file this file will be returned in a single item File array.
Only items matching the filter will be returned

Parameters:
dir - The directory to list items for
filter - The filter
Returns:
A File array with all files and directories.

makeBackup

public static void makeBackup(java.io.File file)
Create a backup file of the specified file. The backup will have the same name as the file but with the appended extension .bak.

Parameters:
file - the file to backup


Copyright © 1998-2005 dmonix.org Peter Nerg, All Rights Reserved.