de.intarsys.tools.file
Class FileTools

java.lang.Object
  extended by de.intarsys.tools.file.FileTools

public class FileTools
extends java.lang.Object

Some utility methods to ease life with File instances.


Nested Class Summary
static class FileTools.Lock
           
 
Field Summary
static java.lang.String DIRECTORY_LOCK
           
 
Method Summary
static void appendFile(java.io.File source, java.io.File destination)
          Concatenate the two files given in source and destination.
static java.io.File checkDirectory(java.io.File dir, boolean create, boolean checkCanRead, boolean checkCanWrite)
          Utility method for checking the availablity of a directory.
static java.io.File checkDirectory(java.lang.String path, boolean create, boolean checkCanRead, boolean checkCanWrite)
           
static void copyBinaryFile(java.io.File source, java.io.File destination)
          Copy the byte content of source to destination.
static void copyFile(java.io.File source, java.io.File destination)
           
static void copyFile(java.io.File source, java.lang.String sourceEncoding, java.io.File destination, java.lang.String destinationEncoding)
          Copy the character content of source to destination.
static void copyRecursively(java.io.File source, java.io.File destination)
           
static java.io.File copyRecursivelyInto(java.io.File source, java.io.File destinationParent, java.lang.String newName)
           
static void createEmptyFile(java.io.File file)
          Create an empty file.
static java.io.File createTempFile(java.io.File file)
          Create a file object representing a temporary file in the user's temp dir with the same name as the given file.
static java.io.File createTempFile(java.lang.String filename)
          Create a file object representing a temporary file in the user's temp dir with the given filename.
static void deleteAfter(java.io.File directory, long millis, boolean recursiveScan)
          Delete any file in directory that is older than millis milliseconds.
static boolean deleteRecursivly(java.io.File file)
          Deletes a file or directory, if necessary recursivly.
static boolean deleteRecursivly(java.io.File file, boolean deleteRoot)
          Deletes a file or directory, if necessary recursivly.
static boolean equalsOnSystem(java.io.File source, java.io.File destination)
          true when the two files represent the same physical file in the file system.
static java.lang.String getBaseName(java.io.File file)
          Get the local name of the file in its directory without the extension.
static java.lang.String getBaseName(java.io.File file, java.lang.String defaultName)
          Get the local name of the file in its directory without the extension.
static java.lang.String getBaseName(java.lang.String filename)
          Get the local name of the file in its directory without the extension.
static java.lang.String getBaseName(java.lang.String filename, java.lang.String defaultName)
          Get the local name of the file in its directory without the extension.
static java.lang.String getEncoding()
           
static java.lang.String getExtension(java.io.File file)
          Get the extension of the file name.
static java.lang.String getExtension(java.lang.String filename)
          Get the extension of the file name.
static java.lang.String getExtension(java.lang.String filename, java.lang.String defaultName)
          Get the extension of the file name.
static java.lang.String getFileName(java.lang.String filename)
          Get the local name of the file in its directory (with extension).
static java.lang.String getFileName(java.lang.String filename, java.lang.String defaultName)
          Get the local name of the file in its directory (with extension).
static java.io.File getParentFile(java.io.File file)
          Try to get a valid parent for file.
static java.lang.String getPathRelativeTo(java.io.File file, java.io.File base)
          get relative path of "file" with respect to "base" directory example : base = /a/b/c; file = /a/d/e/x.txt; getRelativePath(file, base) == ../../d/e/x.txt;
static java.lang.String getPathRelativeTo(java.io.File file, java.io.File base, boolean ifAncestor)
           
static boolean isAncestor(java.io.File parent, java.io.File descendant)
           
static boolean isWindows()
           
static FileTools.Lock lock(java.io.File file)
           
static void renameFile(java.io.File source, java.io.File destination)
           
static void renameFile(java.io.File source, java.lang.String sourceEncoding, java.io.File destination, java.lang.String destinationEncoding)
          "Rename" a file.
static java.io.File resolvePath(java.io.File parent, java.lang.String path)
          Return a new File instance for "path".
static byte[] toBytes(java.io.File file)
          Create a byte array with the files content.
static java.lang.String toString(java.io.File file)
          Read a file's content at once and return as a string.
static java.lang.String toString(java.io.File file, java.lang.String encoding)
          Read a file's content at once and return as a string in the correct encoding.
static java.lang.String trimPath(java.lang.String param)
          Replaces all characters that are generally not allowed or considered useful in filenames with underscore.
protected static void unlock(FileTools.Lock lock)
           
static void wait(java.io.File file, long timeout, long delay)
          Wait for a file to arrive.
static void write(java.io.File file, byte[] bytes)
          Create a file from the byte content.
static void write(java.io.File file, java.lang.String text)
          Create a file from the string content.
static void write(java.io.File file, java.lang.String text, boolean append)
          Create a file from the string content.
static void write(java.io.File file, java.lang.String text, java.lang.String encoding)
          Create a file from the string content.
static void write(java.io.File file, java.lang.String text, java.lang.String encoding, boolean append)
          Create a file from the string content / append a string to a file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DIRECTORY_LOCK

public static final java.lang.String DIRECTORY_LOCK
See Also:
Constant Field Values
Method Detail

appendFile

public static void appendFile(java.io.File source,
                              java.io.File destination)
                       throws java.io.IOException
Concatenate the two files given in source and destination.

Parameters:
source - The file to be appended.
destination - The file to append to.
Throws:
java.io.IOException

checkDirectory

public static java.io.File checkDirectory(java.io.File dir,
                                          boolean create,
                                          boolean checkCanRead,
                                          boolean checkCanWrite)
                                   throws java.io.IOException
Utility method for checking the availablity of a directory.

Parameters:
dir - The directory to check.
create - Flag if we should create if dir not already exists.
checkCanRead - Flag if we should check read permission.
checkCanWrite - Flag if we should check write permission.
Returns:
The checked directory.
Throws:
java.io.IOException

checkDirectory

public static java.io.File checkDirectory(java.lang.String path,
                                          boolean create,
                                          boolean checkCanRead,
                                          boolean checkCanWrite)
                                   throws java.io.IOException
Throws:
java.io.IOException
See Also:
checkDirectory(File, boolean, boolean, boolean)

copyBinaryFile

public static void copyBinaryFile(java.io.File source,
                                  java.io.File destination)
                           throws java.io.IOException
Copy the byte content of source to destination.

Parameters:
source - The file whose contents we should copy.
destination - The file where the contents are copied to.
Throws:
java.io.IOException

copyFile

public static void copyFile(java.io.File source,
                            java.io.File destination)
                     throws java.io.IOException
Throws:
java.io.IOException
See Also:
copyBinaryFile(File, File)

copyFile

public static void copyFile(java.io.File source,
                            java.lang.String sourceEncoding,
                            java.io.File destination,
                            java.lang.String destinationEncoding)
                     throws java.io.IOException
Copy the character content of source to destination.

Parameters:
source - The file whose contents we should copy.
sourceEncoding - The encoding of the source byte stream.
destination - The file where the contents are copied to.
destinationEncoding - The encoding of the destination byte stream.
Throws:
java.io.IOException

copyRecursively

public static void copyRecursively(java.io.File source,
                                   java.io.File destination)
                            throws java.io.IOException
Throws:
java.io.IOException

copyRecursivelyInto

public static java.io.File copyRecursivelyInto(java.io.File source,
                                               java.io.File destinationParent,
                                               java.lang.String newName)
                                        throws java.io.IOException
Throws:
java.io.IOException

createEmptyFile

public static void createEmptyFile(java.io.File file)
                            throws java.io.IOException
Create an empty file.

Parameters:
file -
Throws:
java.io.IOException

createTempFile

public static java.io.File createTempFile(java.io.File file)
                                   throws java.io.IOException
Create a file object representing a temporary file in the user's temp dir with the same name as the given file.

Parameters:
file - file to use
Returns:
file object representing a temporary file
Throws:
java.io.IOException

createTempFile

public static java.io.File createTempFile(java.lang.String filename)
                                   throws java.io.IOException
Create a file object representing a temporary file in the user's temp dir with the given filename.

This does not actually create a file in the file system.

Parameters:
filename - filename to use
Returns:
file object representing a temporary file
Throws:
java.io.IOException

deleteAfter

public static void deleteAfter(java.io.File directory,
                               long millis,
                               boolean recursiveScan)
                        throws java.io.IOException
Delete any file in directory that is older than millis milliseconds. When recursiveScan is true the directory lookup is made recursive.

Parameters:
directory - The directory to scan.
millis - The number of milliseconds a file is allowed to live.
recursiveScan - Flag if we should handle directories recursive.
Throws:
java.io.IOException

deleteRecursivly

public static boolean deleteRecursivly(java.io.File file)
Deletes a file or directory, if necessary recursivly.

Returns true if file could be deleted inclusive its components, otherwise false.

Parameters:
file - The file or directory to delete.
Returns:
true if file could be deleted inclusive its components, otherwise false.

deleteRecursivly

public static boolean deleteRecursivly(java.io.File file,
                                       boolean deleteRoot)
Deletes a file or directory, if necessary recursivly.

Returns true if file could be deleted inclusive its components, otherwise false.

Parameters:
file - The file or directory to delete.
deleteRoot - Flag if the root directory should be deleted itself.
Returns:
true if file could be deleted inclusive its components, otherwise false.

equalsOnSystem

public static boolean equalsOnSystem(java.io.File source,
                                     java.io.File destination)
true when the two files represent the same physical file in the file system.

Parameters:
source - The first file to be checked.
destination - The second file to be checked.
Returns:
true when the two files represent the same physical file in the file system.

getBaseName

public static java.lang.String getBaseName(java.io.File file)
Get the local name of the file in its directory without the extension.

Parameters:
file - The file whose base name is requested.
Returns:
The local name of the file in its directory without the extension.

getBaseName

public static java.lang.String getBaseName(java.io.File file,
                                           java.lang.String defaultName)
Get the local name of the file in its directory without the extension.

Parameters:
file - The file whose base name is requested.
Returns:
The local name of the file in its directory without the extension.

getBaseName

public static java.lang.String getBaseName(java.lang.String filename)
Get the local name of the file in its directory without the extension.

Parameters:
filename - The filename whose base name is requested.
Returns:
The local name of the file in its directory without the extension.

getBaseName

public static java.lang.String getBaseName(java.lang.String filename,
                                           java.lang.String defaultName)
Get the local name of the file in its directory without the extension.

Parameters:
filename - The filename whose base name is requested.
defaultName - returned if filename is null or a empty String
Returns:
The local name of the file in its directory without the extension.

getEncoding

public static java.lang.String getEncoding()

getExtension

public static java.lang.String getExtension(java.io.File file)
Get the extension of the file name. If no extension is present, the empty string is returned.

Parameters:
file - The file whose extension is requested.
Returns:
The extension of the file name. If no extension is present, the empty string is returned.

getExtension

public static java.lang.String getExtension(java.lang.String filename)
Get the extension of the file name. If no extension is present, the empty string is returned.

Parameters:
filename - The filename whose extension is requested.
Returns:
The extension of the file name. If no extension is present, the empty string is returned.

getExtension

public static java.lang.String getExtension(java.lang.String filename,
                                            java.lang.String defaultName)
Get the extension of the file name. If no extension is present, the defaultName is returned.

Parameters:
filename - The filename whose extension is requested.
defaultName - returned if the filename is empty or null or there is no extension
Returns:
The extension of the file name. If no extension is present, the empty string is returned.

getFileName

public static java.lang.String getFileName(java.lang.String filename)
Get the local name of the file in its directory (with extension).

Parameters:
filename - The filename whose name is requested.
Returns:
The local name of the file in its directory (with extension)

getFileName

public static java.lang.String getFileName(java.lang.String filename,
                                           java.lang.String defaultName)
Get the local name of the file in its directory (with extension).

Parameters:
filename - The filename whose name is requested.
defaultName - returned if filename is null or a empty String
Returns:
The local name of the file in its directory (with extension)

getParentFile

public static java.io.File getParentFile(java.io.File file)
Try to get a valid parent for file.

Parameters:
file -

getPathRelativeTo

public static java.lang.String getPathRelativeTo(java.io.File file,
                                                 java.io.File base)
                                          throws java.io.IOException
get relative path of "file" with respect to "base" directory example : base = /a/b/c; file = /a/d/e/x.txt; getRelativePath(file, base) == ../../d/e/x.txt;

Parameters:
base - base path, should be a directory, not a file, or it doesn't make sense
file - file to generate path for
Returns:
path from home to f as a string
Throws:
java.io.IOException

getPathRelativeTo

public static java.lang.String getPathRelativeTo(java.io.File file,
                                                 java.io.File base,
                                                 boolean ifAncestor)

isAncestor

public static boolean isAncestor(java.io.File parent,
                                 java.io.File descendant)

isWindows

public static boolean isWindows()

lock

public static FileTools.Lock lock(java.io.File file)

renameFile

public static void renameFile(java.io.File source,
                              java.io.File destination)
                       throws java.io.IOException
Throws:
java.io.IOException
See Also:
renameFile(File, String, File, String)

renameFile

public static void renameFile(java.io.File source,
                              java.lang.String sourceEncoding,
                              java.io.File destination,
                              java.lang.String destinationEncoding)
                       throws java.io.IOException
"Rename" a file.

The effect is that there is a new file destination, encoded in destinationEncoding, the old file source is deleted.

Parameters:
source - The source name of the file.
sourceEncoding - The encoding of the source file.
destination - The destination name of the file.
destinationEncoding - The encoding of the destination file.
Throws:
java.io.IOException

resolvePath

public static java.io.File resolvePath(java.io.File parent,
                                       java.lang.String path)
Return a new File instance for "path". If path is relative, than it will be interpreted as a child of "parent", if it is absolute, it is returned as is.

ATTENTION: On windows, if "path" is absolute but without drive or UNC prefix, this root information is NOT taken from "parent".

Parameters:
parent -
path -
Returns:
Return a new File instance for "path".

toBytes

public static byte[] toBytes(java.io.File file)
                      throws java.io.IOException
Create a byte array with the files content.

Parameters:
file - The file to read.
Returns:
Create a byte array with the files content.
Throws:
java.io.IOException

toString

public static java.lang.String toString(java.io.File file)
                                 throws java.io.IOException
Read a file's content at once and return as a string.

Use with care!

Parameters:
file - The file to read.
Returns:
The string content of the file.
Throws:
java.io.IOException

toString

public static java.lang.String toString(java.io.File file,
                                        java.lang.String encoding)
                                 throws java.io.IOException
Read a file's content at once and return as a string in the correct encoding.

Use with care!

Parameters:
file - The file to read.
encoding - The encoding to use.
Returns:
The string content of the file.
Throws:
java.io.IOException

trimPath

public static java.lang.String trimPath(java.lang.String param)
Replaces all characters that are generally not allowed or considered useful in filenames with underscore.

Parameters:
param - java.lang.String
Returns:
java.lang.String

unlock

protected static void unlock(FileTools.Lock lock)

wait

public static void wait(java.io.File file,
                        long timeout,
                        long delay)
                 throws java.io.IOException
Wait for a file to arrive.

The method waits at most timeout milliseconds for a file to arrive. When delay is != 0 the method checks the file's size for changes it reaches a stable size.

Parameters:
file - The file to wait for.
timeout - The maximum time in milliseconds to wait for first occurence of file.
delay - The number of milliseconds between two checks against the files size.
Throws:
java.io.IOException

write

public static void write(java.io.File file,
                         byte[] bytes)
                  throws java.io.IOException
Create a file from the byte content.

Parameters:
file - The file to write/create
bytes - The data to be written into the file.
Throws:
java.io.IOException

write

public static void write(java.io.File file,
                         java.lang.String text)
                  throws java.io.IOException
Create a file from the string content.

Parameters:
file - The file to write/create
text - The text to be written into the file.
Throws:
java.io.IOException

write

public static void write(java.io.File file,
                         java.lang.String text,
                         boolean append)
                  throws java.io.IOException
Create a file from the string content.

Parameters:
file - The file to write/create
text - The text to be written into the file.
append - Flag to append to an existing file or create a new file.
Throws:
java.io.IOException

write

public static void write(java.io.File file,
                         java.lang.String text,
                         java.lang.String encoding)
                  throws java.io.IOException
Create a file from the string content.

Parameters:
file - The file to write/create
text - The text to be written into the file.
Throws:
java.io.IOException

write

public static void write(java.io.File file,
                         java.lang.String text,
                         java.lang.String encoding,
                         boolean append)
                  throws java.io.IOException
Create a file from the string content / append a string to a file

Parameters:
file - The file to write/create
text - The text to be written into the file.
append - Flag to append to an existing file or create a new file.
Throws:
java.io.IOException