RbUtils 0.13

org.ktc.rbutils.api.file
Class FileTools

java.lang.Object
  extended by org.ktc.rbutils.api.file.FileTools

public class FileTools
extends Object

General file manipulation utilities.

Since:
RbUtils 0.1.0
Version:
$Revision: 1.5 $
Author:
redfish

Field Summary
static String DOS_SEPARATOR
          The Dos file separator String.
static char DOS_SEPARATOR_CHAR
          The Dos file separator character.
static String EXTENSION_SEPARATOR
          The extension separator String.
static char EXTENSION_SEPARATOR_CHAR
          The extension separator character.
static String JAVA_EXTENSION
          The file extension of a java file.
static String PACKAGE_SEPARATOR
          The package separator String.
static char PACKAGE_SEPARATOR_CHAR
          The package separator character.
static String UNIX_SEPARATOR
          The Unix file separator String.
static char UNIX_SEPARATOR_CHAR
          The Unix file separator character.
 
Constructor Summary
protected FileTools()
          Dummy constructor.
 
Method Summary
static int countFiles(File directory, String[] extensions, boolean recursive)
          Returns the number of files found in a directory.
static boolean equalNormalized(File file1, File file2)
          Checks whether two File are equal after both have been normalized and using the case rules of the system.
static void forceNewFile(File file)
          Cleans and creates a new file in the file system.
static Locale getLocaleFrom(File file)
          Returns the locale of the file according to its filename.
static String getNameWithoutExtension(File file)
          Returns the filename of a File without extension.
static File getNewInstance(File file)
          Get a new instance of a File.
static String getStrippedFileName(File root, File file)
          Create a stripped down version of a filename.
static String getStrippedFileName(File root, File file, boolean forceUnixSeparator)
          Create a stripped down version of a filename.
static String inferPackageClassName(File root, File file, boolean removedLocale)
          Returns the package (if file is a directory) or class (if file is a file in the filesystem) name of a File according to its relative path from a root directory.
static String inferPackageClassName(File root, File file, boolean removedLocale, boolean onlySubPkg)
          Returns the package (if file is a directory) or class (if file is a file in the filesystem) name of a File according to its relative path from a root directory.
static String inferPackageName(File root, File file)
          Returns the package (if file is a directory) or class (if file is a file in the filesystem) name of a File according to its relative path from a root directory.
static boolean isRootParentOfFile(File root, File file)
          Determines if file is in the root directory.
static String normalizeNoEndSeparator(File file)
          Normalizes the absolute path of a File with the {org.apache.commons.io.FilenameUtils#normalizeNoEndSeparator(java.lang.String) normalizeNoEndSeparator()} method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JAVA_EXTENSION

public static final String JAVA_EXTENSION
The file extension of a java file.

See Also:
Constant Field Values

EXTENSION_SEPARATOR_CHAR

public static final char EXTENSION_SEPARATOR_CHAR
The extension separator character.

See Also:
Constant Field Values

EXTENSION_SEPARATOR

public static final String EXTENSION_SEPARATOR
The extension separator String.


PACKAGE_SEPARATOR_CHAR

public static final char PACKAGE_SEPARATOR_CHAR
The package separator character.

See Also:
Constant Field Values

PACKAGE_SEPARATOR

public static final String PACKAGE_SEPARATOR
The package separator String.


UNIX_SEPARATOR_CHAR

public static final char UNIX_SEPARATOR_CHAR
The Unix file separator character.

See Also:
Constant Field Values

UNIX_SEPARATOR

public static final String UNIX_SEPARATOR
The Unix file separator String.


DOS_SEPARATOR_CHAR

public static final char DOS_SEPARATOR_CHAR
The Dos file separator character.

See Also:
Constant Field Values

DOS_SEPARATOR

public static final String DOS_SEPARATOR
The Dos file separator String.

Constructor Detail

FileTools

protected FileTools()
Dummy constructor. This MUST NOT be instanciated.

Throws:
UnsupportedOperationException - if this class is instanciated
Method Detail

inferPackageClassName

public static String inferPackageClassName(File root,
                                           File file,
                                           boolean removedLocale,
                                           boolean onlySubPkg)
                                    throws FileNotFoundException
Returns the package (if file is a directory) or class (if file is a file in the filesystem) name of a File according to its relative path from a root directory.

The name of this package uses the Java language dot notation for the package. i.e java.lang.
It can be the sub-package name (the name of the package which contains this class/package or a full class/package name (name of the sub-package and the name of this class/package).

If root and file denote the same directory, , the returned value is the package separator.
If file is in the root directory and onlySubPkg is true, the empty string is returned.

Parameters:
root - the root directory used to resolved package or class names against file
file - the file used to resolve its package/class name
removedLocale - if true, the locale is removed from the class name; otherwise, the locale is kept (only used if file is a file in the filesytem or onlySubPkg is false)
onlySubPkg - if true, returns only the sub-package name ; otherwise, returns the full class/package name
Returns:
the package name as desired
Throws:
NullArgumentException - if root or file is null
FileNotFoundException - if root does not exist
NotDirectoryException - if root is not a directory

inferPackageClassName

public static String inferPackageClassName(File root,
                                           File file,
                                           boolean removedLocale)
                                    throws FileNotFoundException
Returns the package (if file is a directory) or class (if file is a file in the filesystem) name of a File according to its relative path from a root directory.

The name of this package uses the Java language dot notation for the package. i.e java.lang.
The returned value is the full class/package name (name of the sub-package and the name of this class/package).

Parameters:
root - the root directory used to resolve package or class names against file
file - the file used to resolved its package/class name
removedLocale - if true, the locale is removed from the class name; otherwise, the locale is kept
Returns:
the package name as desired
Throws:
NullArgumentException - if root or file is null
FileNotFoundException - if root does not exist
NotDirectoryException - if root is not a directory

inferPackageName

public static String inferPackageName(File root,
                                      File file)
                               throws FileNotFoundException
Returns the package (if file is a directory) or class (if file is a file in the filesystem) name of a File according to its relative path from a root directory.

The name of this package uses the Java language dot notation for the package. i.e java.lang.
The returned value is the sub-package name (the name of the package which contains this class/package.

If file is the root directory , the returned value is the package separator.
If file is in the root directory , the returned value is the empty string.

Parameters:
root - the root directory used to resolve package or class names against file
file - the file used to resolved its package/class name
Returns:
the package name as desired
Throws:
NullArgumentException - if root or file is null
FileNotFoundException - if root does not exist
NotDirectoryException - if root is not a directory

getNameWithoutExtension

public static String getNameWithoutExtension(File file)
Returns the filename of a File without extension.

Parameters:
file - the File from which the name without extension is desired
Returns:
the filename without extension
Throws:
NullArgumentException - if file is null

getStrippedFileName

public static String getStrippedFileName(File root,
                                         File file)
                                  throws FileNotFoundException
Create a stripped down version of a filename.

Parameters:
root - the root directory used to resolve the stripped file name against file
file - the file used to resolve its stripped file name
Returns:
the filename where an initial basedir is stripped
Throws:
NullArgumentException - if root or file is null
FileNotFoundException - if root does not exist
NotDirectoryException - if root is not a directory

getStrippedFileName

public static String getStrippedFileName(File root,
                                         File file,
                                         boolean forceUnixSeparator)
                                  throws FileNotFoundException
Create a stripped down version of a filename.

If file is in root, returns an empty String.

Parameters:
root - the root directory used to resolved the stripped file name against file
file - the file used to resolve its stripped file name
forceUnixSeparator - if true, the returned value uses the UNIX_SEPARATOR_CHAR as file separator; otherwise, it uses the system file separator
Returns:
the filename where an initial basedir is stripped
Throws:
NullArgumentException - if root or file is null
FileNotFoundException - if root does not exist
NotDirectoryException - if root is not a directory
NotRootParentOfFileException - if file is not in root

isRootParentOfFile

public static boolean isRootParentOfFile(File root,
                                         File file)
                                  throws FileNotFoundException
Determines if file is in the root directory.

Parameters:
root - the directory
file - the file (as file in the file system)
Returns:
true if file is in the root directory; otherwise false
Throws:
NullArgumentException - if root or file is null
FileNotFoundException - if root does not exist
NotDirectoryException - if root is not a directory

getLocaleFrom

public static Locale getLocaleFrom(File file)
                            throws FileNotFoundException
Returns the locale of the file according to its filename. Note: this method will work as expected if and only if the filename of the File does not contain the underscore character '_' (except for the locale part).

Parameters:
file - file from which the locale is desired
Returns:
the locale of the file according to the filename
Throws:
NullArgumentException - if file is null
FileNotFoundException - if file does not exist
NotFileException - if file is not a file in the file system

getNewInstance

public static File getNewInstance(File file)
Get a new instance of a File.

Parameters:
file - a file from which a new instance is desired
Returns:
the new instance
Throws:
NullArgumentException - if file is null

equalNormalized

public static boolean equalNormalized(File file1,
                                      File file2)
Checks whether two File are equal after both have been normalized and using the case rules of the system.

Both filenames are first passed to normalizeNoEndSeparator(File). The check is then performed case-sensitive on Unix and case-insensitive on Windows.

Parameters:
file1 - the first file to query
file2 - the second file to query
Returns:
true if the two File are equals; false otherwise
Throws:
NullArgumentException - if file1 or file2 is null

normalizeNoEndSeparator

public static String normalizeNoEndSeparator(File file)
Normalizes the absolute path of a File with the {org.apache.commons.io.FilenameUtils#normalizeNoEndSeparator(java.lang.String) normalizeNoEndSeparator()} method.

Parameters:
file - the file to be normalized.
Returns:
the normalized absolute path.
Throws:
NullArgumentException - if file is null
See Also:
FilenameUtils.normalizeNoEndSeparator(java.lang.String)

countFiles

public static int countFiles(File directory,
                             String[] extensions,
                             boolean recursive)
                      throws FileNotFoundException
Returns the number of files found in a directory.

Parameters:
directory - the directory to search in.
extensions - an array of extensions, ex. {"java","xml"}. If this parameter is null, all files are accepted.
recursive - If true all subdirectories are searched, too.
Returns:
the number of found files.
Throws:
NullArgumentException - if directory is null
FileNotFoundException - if directory does not exist
NotDirectoryException - if directory is not a directory
Since:
RbUtils 0.9.3.3

forceNewFile

public static void forceNewFile(File file)
                         throws IOException
Cleans and creates a new file in the file system.

The previous file is deleted if it exists. All parent directories of the file are first created. Finally, a new file is created.

Parameters:
file - the file to be created.
Throws:
IOException - if an error occurs on file deletion, on file creation, or on parent directories creation.

Back to the RbUtils Home Page

Copyright © 2005-2009 RbUtils Project. All Rights Reserved.