deegree-core-3.0-pre-nightly (2010/03/05 00:01) - official

org.deegree.commons.utils
Class FileUtils

java.lang.Object
  extended by org.deegree.commons.utils.FileUtils

public class FileUtils
extends Object

This class contains static utility methods for handling files and filenames.

Version:
$Revision: 22809 $, $Date: 2010-03-03 17:26:54 +0100 (Mi, 03 Mrz 2010) $
Author:
Oliver Tonnhofer, Markus Schneider, last edited by: $Author: rbezema $

Constructor Summary
FileUtils()
           
 
Method Summary
static List<File> findFilesForExtensions(File topDirectory, boolean recurse, String patternList)
          Find the files in the given directory (and sub-directories) which match the given extension pattern(s).
static File getAsFile(URL url)
          Converts a file:/...
static String getBasename(File file)
          Returns the filename, without any extension.
static String getFileExtension(File file)
          Returns the file extension (Eg.
static String getFilename(File file)
          Returns the filename, without any extension and path (Eg.
static URL loadDeegreeConfiguration(Class<?> configurationClass, String fileName)
          A helper method which will try to load the given configuration file (name) from the root directory before trying to load it from the package of the given configuration class.
static void writeBinaryFile(File file, byte[] data)
          Writes the given binary data to the specified file.
static void writeBinaryTempFile(String filePrefix, String fileSuffix, byte[] data)
          Writes the given binary data to a temporary file (created from specified prefix and suffix).
static void writeFile(File file, String content)
          Writes the given String to the specified file.
static void writeTempFile(String filePrefix, String fileSuffix, String content)
          Writes the given String to a temporary file (created from specified prefix and suffix).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtils

public FileUtils()
Method Detail

getFilename

public static String getFilename(File file)
Returns the filename, without any extension and path (Eg. /tmp/foo.txt -> foo)

Parameters:
file -
Returns:
the basename or null if the file is null;

getBasename

public static String getBasename(File file)
Returns the filename, without any extension. (Eg. /tmp/foo.txt -> /tmp/foo)

Parameters:
file -
Returns:
the basename

getFileExtension

public static String getFileExtension(File file)
Returns the file extension (Eg. /tmp/foo.txt -> txt)

Parameters:
file -
Returns:
the file extension

loadDeegreeConfiguration

public static URL loadDeegreeConfiguration(Class<?> configurationClass,
                                           String fileName)
A helper method which will try to load the given configuration file (name) from the root directory before trying to load it from the package of the given configuration class. Consider following example:

The org.deegree.geometry.GeometryFactory wants to load the File geometry_config.xml located in org.deegree.geometry.configuration (hence the filename will be configuration/geometry_config.xml)
. This method will first try to read geometry_config.xml (without the 'configuration' directory from the given fileName) from the root directory '/' (e.g. WEB-INF/classes in a serlvet environment)
If this was unsuccessful this method will try to load the file from the given packageName with the relative fileName appended to it.

Parameters:
configurationClass - will be used to read the stream from.
fileName - name of the file to read.
Returns:
the given file handle, or null if the given file could not be read (in either location) or either of the given parameters is null.

writeFile

public static void writeFile(File file,
                             String content)
Writes the given String to the specified file.

Parameters:
file - file to write to
content -

writeTempFile

public static void writeTempFile(String filePrefix,
                                 String fileSuffix,
                                 String content)
Writes the given String to a temporary file (created from specified prefix and suffix).

Parameters:
filePrefix - prefix for the temp file name, must be at least three characters long
fileSuffix - suffix for the temp file name, can be null (then ".tmp" is used)
content -
See Also:
File.createTempFile(String, String)

writeBinaryFile

public static void writeBinaryFile(File file,
                                   byte[] data)
Writes the given binary data to the specified file.

Parameters:
file - file to write to
data - binary data to be written

writeBinaryTempFile

public static void writeBinaryTempFile(String filePrefix,
                                       String fileSuffix,
                                       byte[] data)
Writes the given binary data to a temporary file (created from specified prefix and suffix).

Parameters:
filePrefix - prefix for the temp file name, must be at least three characters long
fileSuffix - suffix for the temp file name, can be null (then ".tmp" is used)
data - binary data to be written
See Also:
File.createTempFile(String, String)

getAsFile

public static File getAsFile(URL url)
                      throws IllegalArgumentException
Converts a file:/... URL into a file.

NOTE: The implementation uses an idea from Kohsuke Kawaguchi.

Parameters:
url - file:/... URL
Returns:
corresponding file object
Throws:
IllegalArgumentException - if the given URL is not a file:/... URL

findFilesForExtensions

public static List<File> findFilesForExtensions(File topDirectory,
                                                boolean recurse,
                                                String patternList)
Find the files in the given directory (and sub-directories) which match the given extension pattern(s).

Parameters:
topDirectory - to start the search from.
recurse -
patternList - comma seperated list of extensions to search for.
Returns:
the list of files matching the given extension.

deegree-core-3.0-pre-nightly (2010/03/05 00:01) - official

an open source project founded by lat/lon, Bonn, Germany.
For more information visit: http://www.deegree.org

]]>