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: 30249 $, $Date: 2011-03-28 17:19:06 +0200 (Mon, 28 Mar 2011) $
Author:
Oliver Tonnhofer, Markus Schneider, last edited by: $Author: aschmitz $

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 String getSystemCharset()
          returns the name of the charset that is passed to the JVM as system property -DCHARSET=...
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 StringBuffer readTextFile(InputStream is)
          reads a Text file from its resource.
static StringBuffer readTextFile(Reader reader)
          reads a Text file from its resource.
static StringBuffer readTextFile(URL url)
          reads a Text file from its resource.
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.

readTextFile

public static StringBuffer readTextFile(URL url)
                                 throws IOException
reads a Text file from its resource. For accessing the resource an InputStreamReader with encoding read from CharsetUtils.getSystemCharset()

Parameters:
url -
Returns:
contents of the url as a StringBuffer
Throws:
IOException

readTextFile

public static StringBuffer readTextFile(InputStream is)
                                 throws IOException
reads a Text file from its resource. For accessing the resource an InputStreamReader with encoding read from CharsetUtils.getSystemCharset()

Parameters:
is -
Returns:
contents of the input stream as a StringBuffer
Throws:
IOException

readTextFile

public static StringBuffer readTextFile(Reader reader)
                                 throws IOException
reads a Text file from its resource.

Parameters:
reader -
Returns:
contents of the reader as a StringBuffer
Throws:
IOException

getSystemCharset

public static String getSystemCharset()
returns the name of the charset that is passed to the JVM as system property -DCHARSET=... If no charset has been defined UTF-8 will be returned as default.

Returns:
the name of the charset that is passed to the JVM as system property -DCHARSET=... If no charset has been defined UTF-8 will be returned as default.


Copyright © 2011. All Rights Reserved.