de.enough.polish.util
Class FileUtil

java.lang.Object
  extended by de.enough.polish.util.FileUtil

public final class FileUtil
extends Object

Provides some often used methods for handling files.

Copyright Enough Software 2004 - 2011

 history
        14-Jan-2004 - rob creation
 

Author:
Robert Virkus, robert@enough.de

Constructor Summary
FileUtil()
           
 
Method Summary
static void addLine(File file, String line)
          Adds the given line to the specified textfile.
static void addLines(File file, String[] lines)
          Adds the given line to the specified textfile.
static void copy(File[] files, File targetDir)
          Copies the given files to the specified target directory.
static void copy(File[] files, File targetDir, boolean overwrite)
          Copies the given files to the specified target directory.
static void copy(File source, File target)
          Copies a file.
static void copyDirectoryContents(File directory, File targetDir, boolean update)
          Copies the contents of a directory to the specified target directory.
static void copyDirectoryContents(File directory, String targetDirName, boolean update)
          Copies the contents of a directory to the specified target directory.
static boolean delete(File file)
          Deletes a file or a directory.
static String[] filterDirectory(File dir, String extension, boolean recursive)
          Retrieves all files from the given directory
static String[] filterDirectory(File dir, String extension, FilenameFilter filenameFilter, boolean recursive)
           
static byte[] getBytesFromFile(File file)
          Extracts the bytes from a file.
static Map<String,String> readProperties(File file)
          Reads properties from the given file.
static Map<String,String> readProperties(InputStream in)
          Reads properties from the given input stream.
static void readProperties(InputStream in, char delimiter, char comment, Map<String,String> properties)
          Reads properties from the given input stream.
static void readProperties(InputStream in, char delimiter, char comment, Map<String,String> properties, boolean ignoreInvalidProperties)
          Reads properties from the given input stream.
static void readProperties(InputStream in, char delimiter, char comment, Map<String,String> properties, boolean ignoreInvalidProperties, String encoding)
          Reads properties from the given input stream.
static void readProperties(InputStream in, char delimiter, char comment, Map<String,String> properties, boolean ignoreInvalidProperties, String encoding, boolean translateToAscii, boolean translateToNative)
          Reads properties from the given input stream.
static void readProperties(InputStream in, char delimiter, Map<String,String> properties)
          Reads properties from the given input stream.
static void readProperties(InputStream in, char delimiter, Map<String,String> properties, String encoding)
          Reads properties from the given input stream.
static void readProperties(InputStream in, char delimiter, Map<String,String> properties, String encoding, boolean translateToAscii, boolean translateToNative)
          Reads properties from the given input stream.
static Map<String,String> readProperties(Reader reader)
          Reads properties from the given reader.
static void readProperties(Reader reader, char delimiter, char comment, Map<String,String> properties, boolean ignoreInvalidProperties)
          Reads properties from the given reader.
static void readProperties(Reader reader, char delimiter, char comment, Map<String,String> properties, boolean ignoreInvalidProperties, boolean translateToAscii, boolean translateToNative)
          Reads properties from the given reader.
static HashMap<String,String> readPropertiesFile(File file)
          Reads a properties file.
static HashMap<String,String> readPropertiesFile(File file, char delimiter)
          Reads a properties file.
static void readPropertiesFile(File file, char delimiter, char comment, Map<String,String> map, boolean ignoreInvalidProperties)
          Reads a properties file.
static void readPropertiesFile(File file, char delimiter, Map<String,String> map)
          Reads a properties file.
static String[] readTextFile(File file)
          Reads a text file.
static String[] readTextFile(File file, String encoding)
          Reads a text file.
static String[] readTextFile(InputStream in)
          Reads the text from the given input stream in the default encoding.
static String[] readTextFile(InputStream in, String encoding)
          Reads the text from the given input stream in the default encoding.
static String[] readTextFile(String fileName)
          Reads a text file.
static void writePropertiesFile(File file, char delimiter, Map<String,String> properties)
          Writes the properties which are defined in the given HashMap into a textfile.
static void writePropertiesFile(File file, Map<String,String> properties)
          Writes the properties which are defined in the given HashMap into a textfile.
static void writeTextFile(File file, Collection<String> lines)
          Writes (and creates) a text file.
static void writeTextFile(File file, String[] lines)
          Writes (and creates) a text file.
static void writeTextFile(File file, String[] lines, String encoding)
          Writes the given textlines into the specified file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

public FileUtil()
Method Detail

readTextFile

public static String[] readTextFile(String fileName)
                             throws FileNotFoundException,
                                    IOException
Reads a text file.

Parameters:
fileName - the name of the text file
Returns:
the lines of the text file
Throws:
FileNotFoundException - when the file was not found
IOException - when file could not be read.

readTextFile

public static String[] readTextFile(File file)
                             throws FileNotFoundException,
                                    IOException
Reads a text file.

Parameters:
file - the text file
Returns:
the lines of the text file
Throws:
FileNotFoundException - when the file was not found
IOException - when file could not be read.

readTextFile

public static String[] readTextFile(File file,
                                    String encoding)
                             throws FileNotFoundException,
                                    IOException
Reads a text file.

Parameters:
file - the text file
encoding - the encoding of the textfile
Returns:
the lines of the text file
Throws:
FileNotFoundException - when the file was not found
IOException - when file could not be read.

readTextFile

public static String[] readTextFile(InputStream in)
                             throws IOException
Reads the text from the given input stream in the default encoding.

Parameters:
in - the input stream
Returns:
the text contained in the stream
Throws:
IOException - when stream could not be read.

readTextFile

public static String[] readTextFile(InputStream in,
                                    String encoding)
                             throws IOException
Reads the text from the given input stream in the default encoding.

Parameters:
in - the input stream
encoding - the encoding of the textfile
Returns:
the text contained in the stream
Throws:
IOException - when stream could not be read.

writeTextFile

public static void writeTextFile(File file,
                                 Collection<String> lines)
                          throws IOException
Writes (and creates) a text file.

Parameters:
file - the file to which the text should be written
lines - the text lines of the file in a collection with String-values
Throws:
IOException - when there is an input/output error during the saving

writeTextFile

public static void writeTextFile(File file,
                                 String[] lines)
                          throws IOException
Writes (and creates) a text file.

Parameters:
file - the file to which the text should be written
lines - the text lines of the file
Throws:
IOException - when there is an input/output error during the saving

copy

public static void copy(File[] files,
                        File targetDir)
                 throws FileNotFoundException,
                        IOException
Copies the given files to the specified target directory.

Parameters:
files - The files which should be copied, when an array element is null, it will be ignored.
targetDir - The directory to which the given files should be copied to.
Throws:
FileNotFoundException - when the source file was not found
IOException - when there is an error while copying the file.
NullPointerException - when files or targetDir is null.

copy

public static void copy(File[] files,
                        File targetDir,
                        boolean overwrite)
                 throws FileNotFoundException,
                        IOException
Copies the given files to the specified target directory.

Parameters:
files - The files which should be copied, when an array element is null, it will be ignored.
targetDir - The directory to which the given files should be copied to.
overwrite - true when existing target files should be overwritten even when they are newer
Throws:
FileNotFoundException - when the source file was not found
IOException - when there is an error while copying the file.
NullPointerException - when files or targetDir is null.

copy

public static void copy(File source,
                        File target)
                 throws FileNotFoundException,
                        IOException
Copies a file.

Parameters:
source - The file which should be copied
target - The file or directory to which the source-file should be copied to.
Throws:
FileNotFoundException - when the source file was not found
IOException - when there is an error while copying the file.

writePropertiesFile

public static void writePropertiesFile(File file,
                                       Map<String,String> properties)
                                throws IOException
Writes the properties which are defined in the given HashMap into a textfile. The notation in the file will be [name]=[value]\n for each defined property.

Parameters:
file - the file which should be created or overwritten
properties - the properties which should be written.
Throws:
IOException - when there is an input/output error during the saving

writePropertiesFile

public static void writePropertiesFile(File file,
                                       char delimiter,
                                       Map<String,String> properties)
                                throws IOException
Writes the properties which are defined in the given HashMap into a textfile. The notation in the file will be [name]=[value]\n for each defined property.

Parameters:
file - the file which should be created or overwritten
delimiter - the character that separates a property-name from a property-value.
properties - the properties which should be written.
Throws:
IOException - when there is an input/output error during the saving

readPropertiesFile

public static HashMap<String,String> readPropertiesFile(File file)
                                                 throws FileNotFoundException,
                                                        IOException
Reads a properties file. The notation of the file needs to be "[name]=[value]\n" for each defined property.

Parameters:
file - the file containing the properties
Returns:
a hashmap containing all properties found in the file
Throws:
FileNotFoundException - when the file was not found
IOException - when file could not be read.
IllegalArgumentException - when the line does not contain a property

readPropertiesFile

public static HashMap<String,String> readPropertiesFile(File file,
                                                        char delimiter)
                                                 throws FileNotFoundException,
                                                        IOException
Reads a properties file. The notation of the file needs to be "[name]=[value]\n" for each defined property.

Parameters:
file - the file containing the properties
delimiter - the character that separates a property-name from a property-value.
Returns:
a hashmap containing all properties found in the file
Throws:
FileNotFoundException - when the file was not found
IOException - when file could not be read.
IllegalArgumentException - when the line does not contain a property

readPropertiesFile

public static void readPropertiesFile(File file,
                                      char delimiter,
                                      Map<String,String> map)
                               throws FileNotFoundException,
                                      IOException
Reads a properties file. The notation of the file needs to be "[name]=[value]\n" where '=' is the defined delimiter character. for each defined property.

Parameters:
file - the file containing the properties
delimiter - the character that separates a property-name from a property-value.
map - the hash map to which the properties should be added
Throws:
FileNotFoundException - when the file was not found
IOException - when file could not be read.
IllegalArgumentException - when the line does not contain a property

readPropertiesFile

public static void readPropertiesFile(File file,
                                      char delimiter,
                                      char comment,
                                      Map<String,String> map,
                                      boolean ignoreInvalidProperties)
                               throws FileNotFoundException,
                                      IOException
Reads a properties file. The notation of the file needs to be "[name]=[value]\n" where '=' is the defined delimiter character. for each defined property.

Parameters:
file - the file containing the properties
delimiter - the character that separates a property-name from a property-value.
comment - the character that introduces a comment, e.g. '#'
map - the hash map to which the properties should be added
ignoreInvalidProperties - when this flag is true, invalid property definition (those that do not contain the delimiter char) are ignored
Throws:
FileNotFoundException - when the file was not found
IOException - when file could not be read.
IllegalArgumentException - when the line does not contain a property

copyDirectoryContents

public static void copyDirectoryContents(File directory,
                                         String targetDirName,
                                         boolean update)
                                  throws IOException
Copies the contents of a directory to the specified target directory.

Parameters:
directory - the directory containing files
targetDirName - the directory to which the files should be copied to
update - is true when files should be only copied when the source files are newer compared to the target files.
Throws:
IOException - when a file could not be copied
IllegalArgumentException - when the directory is not a directory.

copyDirectoryContents

public static void copyDirectoryContents(File directory,
                                         File targetDir,
                                         boolean update)
                                  throws IOException
Copies the contents of a directory to the specified target directory.

Parameters:
directory - the directory containing files
targetDir - the directory to which the files should be copied to
update - set to true when files should be only copied when the source files are newer compared to the target files.
Throws:
IOException - when a file could not be copied
IllegalArgumentException - when the directory is not a directory.

delete

public static boolean delete(File file)
Deletes a file or a directory.

Parameters:
file - the file or directory which should be deleted.
Returns:
true when the file could be deleted

readProperties

public static Map<String,String> readProperties(InputStream in)
                                         throws IOException
Reads properties from the given input stream.

Parameters:
in - the input stream
Returns:
a map containing all properties that could be read from the input stream
Throws:
IOException - when reading from the input stream fails

readProperties

public static Map<String,String> readProperties(File file)
                                         throws IOException
Reads properties from the given file.

Parameters:
file - the file containing properties separated with '='
Returns:
a map containing all properties that could be read from the input stream
Throws:
IOException - when reading from the file fails
FileNotFoundException - when the file does not exist

readProperties

public static Map<String,String> readProperties(Reader reader)
                                         throws IOException
Reads properties from the given reader.

Parameters:
reader - the input reader
Returns:
a map containing all properties that could be read from the reader
Throws:
IOException - when reading fails

readProperties

public static void readProperties(InputStream in,
                                  char delimiter,
                                  Map<String,String> properties)
                           throws IOException
Reads properties from the given input stream.

Parameters:
in - the input stream
delimiter - the character that separates a property-name from a property-value.
properties - a map containing properties
Throws:
IOException - when reading from the input stream fails

readProperties

public static void readProperties(InputStream in,
                                  char delimiter,
                                  Map<String,String> properties,
                                  String encoding)
                           throws IOException
Reads properties from the given input stream.

Parameters:
in - the input stream
delimiter - the character that separates a property-name from a property-value.
properties - a map containing properties
encoding - the encoding of the file
Throws:
IOException - when reading from the input stream fails

readProperties

public static void readProperties(InputStream in,
                                  char delimiter,
                                  Map<String,String> properties,
                                  String encoding,
                                  boolean translateToAscii,
                                  boolean translateToNative)
                           throws IOException
Reads properties from the given input stream.

Parameters:
in - the input stream
delimiter - the character that separates a property-name from a property-value.
properties - a map containing properties
encoding - the encoding of the file
translateToAscii - true when the FileUtil should translate the code into ASCII only code (using unicode).
translateToNative - true when escape sequences like \t or \n should be converted to native characters
Throws:
IOException - when reading from the input stream fails

readProperties

public static void readProperties(InputStream in,
                                  char delimiter,
                                  char comment,
                                  Map<String,String> properties)
                           throws IOException
Reads properties from the given input stream.

Parameters:
in - the input stream
delimiter - the character that separates a property-name from a property-value.
comment - the char denoting comments
properties - a map containing properties
Throws:
IOException - when reading from the input stream fails

readProperties

public static void readProperties(InputStream in,
                                  char delimiter,
                                  char comment,
                                  Map<String,String> properties,
                                  boolean ignoreInvalidProperties)
                           throws IOException
Reads properties from the given input stream.

Parameters:
in - the input stream
delimiter - the character that separates a property-name from a property-value.
comment - the char denoting comments
properties - a map containing properties
ignoreInvalidProperties - when this flag is true, invalid property definition (those that do not contain the delimiter char) are ignored
Throws:
IOException - when reading from the input stream fails
IllegalArgumentException - when an invalid property definition is encountered and ignoreInvalidProperties is false

readProperties

public static void readProperties(InputStream in,
                                  char delimiter,
                                  char comment,
                                  Map<String,String> properties,
                                  boolean ignoreInvalidProperties,
                                  String encoding)
                           throws IOException
Reads properties from the given input stream.

Parameters:
in - the input stream
delimiter - the character that separates a property-name from a property-value.
comment - the char denoting comments
properties - a map containing properties
ignoreInvalidProperties - when this flag is true, invalid property definition (those that do not contain the delimiter char) are ignored
encoding - the encoding of the text file, when null the default charset is used
Throws:
IOException - when reading from the input stream fails
IllegalArgumentException - when an invalid property definition is encountered and ignoreInvalidProperties is false

readProperties

public static void readProperties(Reader reader,
                                  char delimiter,
                                  char comment,
                                  Map<String,String> properties,
                                  boolean ignoreInvalidProperties)
                           throws IOException
Reads properties from the given reader.

Parameters:
reader - the input reader
delimiter - the character that separates a property-name from a property-value.
comment - the char denoting comments
properties - a map containing properties
ignoreInvalidProperties - when this flag is true, invalid property definition (those that do not contain the delimiter char) are ignored
Throws:
IOException - when reading from the input stream fails
IllegalArgumentException - when an invalid property definition is encountered and ignoreInvalidProperties is false

readProperties

public static void readProperties(InputStream in,
                                  char delimiter,
                                  char comment,
                                  Map<String,String> properties,
                                  boolean ignoreInvalidProperties,
                                  String encoding,
                                  boolean translateToAscii,
                                  boolean translateToNative)
                           throws IOException
Reads properties from the given input stream.

Parameters:
in - the input stream
delimiter - the character that separates a property-name from a property-value.
comment - the char denoting comments
properties - a map containing properties
ignoreInvalidProperties - when this flag is true, invalid property definition (those that do not contain the delimiter char) are ignored
encoding - the encoding of the text file, when null the default charset is used
translateToAscii - true when the FileUtil should translate the code into ASCII only code (using unicode).
translateToNative - true when escape sequences like \t or \n should be converted to native characters
Throws:
IOException - when reading from the input stream fails
IllegalArgumentException - when an invalid property definition is encountered and ignoreInvalidProperties is false

readProperties

public static void readProperties(Reader reader,
                                  char delimiter,
                                  char comment,
                                  Map<String,String> properties,
                                  boolean ignoreInvalidProperties,
                                  boolean translateToAscii,
                                  boolean translateToNative)
                           throws IOException
Reads properties from the given reader.

Parameters:
reader - the input reader
delimiter - the character that separates a property-name from a property-value.
comment - the char denoting comments
properties - a map containing properties
ignoreInvalidProperties - when this flag is true, invalid property definition (those that do not contain the delimiter char) are ignored
translateToAscii - true when the FileUtil should translate the code into ASCII only code (using unicode).
translateToNative - true when escape sequences like \t or \n should be converted to native characters
Throws:
IOException - when reading from the input stream fails
IllegalArgumentException - when an invalid property definition is encountered and ignoreInvalidProperties is false

writeTextFile

public static void writeTextFile(File file,
                                 String[] lines,
                                 String encoding)
                          throws IOException
Writes the given textlines into the specified file.

Parameters:
file - the file to which the text should be written
lines - the text lines of the file
encoding - the encoding, e.g. "UTF8", null when the default encoding should be used
Throws:
IOException - when there is an input/output error during the saving

addLine

public static void addLine(File file,
                           String line)
                    throws IOException
Adds the given line to the specified textfile. The file is created if necessary.

Parameters:
file - the text file
line - the line
Throws:
IOException - when adding fails.

addLines

public static void addLines(File file,
                            String[] lines)
                     throws IOException
Adds the given line to the specified textfile. The file is created if necessary.

Parameters:
file - the text file
lines - the lines that should be added
Throws:
IOException - when adding fails.

filterDirectory

public static String[] filterDirectory(File dir,
                                       String extension,
                                       boolean recursive)
Retrieves all files from the given directory

Parameters:
dir - the directory
extension - the file extension, when the extension is null, all files are included
recursive - true when subdirectories should also be read.
Returns:
an String array with the file-names relative to the given directory that do have the given extension

filterDirectory

public static String[] filterDirectory(File dir,
                                       String extension,
                                       FilenameFilter filenameFilter,
                                       boolean recursive)

getBytesFromFile

public static byte[] getBytesFromFile(File file)
                               throws IOException
Extracts the bytes from a file.

Parameters:
file - the file from which the bytes should be extracted from
Returns:
a byte arry corresponding to the file. Is never null.
Throws:
IOException