org.ow2.util.file
Class FileUtils

java.lang.Object
  extended by org.ow2.util.file.FileUtils

public final class FileUtils
extends Object

This class manages operation done many times on files, like copying them.

Author:
Florent Benoit

Method Summary
static String calculateChecksum(File file, String algorithm)
          For a given file, calculate its checksum using a given algorithm.
static boolean chmod(File file, String mode)
          Change the permissions of a given file by using chmod.
static void close(Closeable... closeables)
          Close all given Closeable instances.
static void copyDirectory(File src, File dest)
          Copy a directory recursively.
static File copyDirectory(File src, File dest, boolean override)
          Copy a directory recursively.
static void copyDirectory(String src, String dest)
          Copy a directory recursively.
static File copyFile(File src, File dest)
          Copy a file and return its copy.
static File copyFile(String src, String dest)
          Copy a file and return its copy.
static boolean delete(File f)
           
static boolean delete(String path)
           
static void dump(InputStream in, File entryFile)
          Write the given input stream in the given file.
static Collection<File> find(File directory, FileFilter fileFilter, boolean recursive)
           
static void flush(Flushable... flushables)
          Flush all given Flushable instances.
static String getMode(File file)
          Return the mode for a given file.
static String lastModifiedFileName(File file)
          For a given file, get the shorter name containing the last modification date.
static void print(File txtFile)
          Print a text file in standard output.
static void print(File txtFile, PrintWriter printWriter)
          Print a text file in a print writer.
static String readFile(String path)
          Return the string contained in the given file, by using the default charset.
static String readFile(String path, Charset charset)
          Return the string contained in the given file, by using the given charset to decode it.
static void unpack(JarFile packedJar, File dest)
          Unpack the source archive in a given directory.
static void updateAttributesInManifest(String jarName, Map<String,String> attributes)
          Change the attribute value in a jar file (and update file if required).
static void updateManifestAttributesInJarArchive(File file, Map<String,String> attributes)
          Change the attribute value in a jar file (and update file if required).
static void updateManifestAttributesInJarDirectory(File file, Map<String,String> attributes)
          Change the attribute value in a jar file (and update directory if required).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

unpack

public static void unpack(JarFile packedJar,
                          File dest)
                   throws FileUtilsException
Unpack the source archive in a given directory.

Parameters:
packedJar - source JarFile to be unpacked
dest - the destination folder
Throws:
FileUtilsException - When unpack fails

dump

public static void dump(InputStream in,
                        File entryFile)
                 throws FileUtilsException
Write the given input stream in the given file.

Parameters:
in - the inputStream to copy.
entryFile - the file where the inputStream must be dumped.
Throws:
FileUtilsException - if the dump failed.

print

public static void print(File txtFile,
                         PrintWriter printWriter)
                  throws FileUtilsException
Print a text file in a print writer.

Parameters:
txtFile - the text file
printWriter - the print writer
Throws:
FileUtilsException - if error occurs

print

public static void print(File txtFile)
                  throws FileUtilsException
Print a text file in standard output.

Parameters:
txtFile - the text file
Throws:
FileUtilsException - if error occurs

copyFile

public static File copyFile(String src,
                            String dest)
                     throws FileUtilsException
Copy a file and return its copy.

Parameters:
src - source file
dest - dest file
Returns:
the copied file
Throws:
FileUtilsException - if the copy of the file failed

copyFile

public static File copyFile(File src,
                            File dest)
                     throws FileUtilsException
Copy a file and return its copy.

Parameters:
src - source file
dest - dest file
Returns:
the copied file
Throws:
FileUtilsException - if the copy of the file failed

delete

public static boolean delete(String path)
Parameters:
path - file/directory to be deleted
Returns:
true if deletion was OK

delete

public static boolean delete(File f)
Parameters:
f - file/directory to be deleted
Returns:
true if deletion was OK

copyDirectory

public static void copyDirectory(String src,
                                 String dest)
                          throws FileUtilsException
Copy a directory recursively. The destination directory will be overridden.

Parameters:
src - source directory
dest - dest directory
Throws:
FileUtilsException - if the copy of the directory failed

copyDirectory

public static File copyDirectory(File src,
                                 File dest,
                                 boolean override)
                          throws FileUtilsException
Copy a directory recursively.

Parameters:
src - source directory
dest - dest directory
override - true if the destination directory is overridden
Returns:
the copied directory
Throws:
FileUtilsException - if the copy of the directory failed

copyDirectory

public static void copyDirectory(File src,
                                 File dest)
                          throws FileUtilsException
Copy a directory recursively. The destination directory will be overridden.

Parameters:
src - source directory
dest - dest directory
Throws:
FileUtilsException - if the copy of the directory failed

updateAttributesInManifest

public static void updateAttributesInManifest(String jarName,
                                              Map<String,String> attributes)
                                       throws FileUtilsException
Change the attribute value in a jar file (and update file if required).

Parameters:
jarName - output jar file
attributes - a list of attributes (name <-> value)
Throws:
FileUtilsException - if the manifest is not updated.

updateManifestAttributesInJarDirectory

public static void updateManifestAttributesInJarDirectory(File file,
                                                          Map<String,String> attributes)
                                                   throws FileUtilsException
Change the attribute value in a jar file (and update directory if required).

Parameters:
file - input archive
attributes - a list of attributes (name <-> value)
Throws:
FileUtilsException - if the given file is not a directory (it may be an archive) or if the inner Manifest opened/updated.

updateManifestAttributesInJarArchive

public static void updateManifestAttributesInJarArchive(File file,
                                                        Map<String,String> attributes)
                                                 throws FileUtilsException
Change the attribute value in a jar file (and update file if required).

Parameters:
file - input archive
attributes - a list of attributes (name <-> value)
Throws:
FileUtilsException - if the given file is not a file (it may be a directory) or if the given jar cannot be opened/updated/closed.

close

public static void close(Closeable... closeables)
Close all given Closeable instances. Ignore IOException that may be thrown.

Parameters:
closeables - list of Closeable instance to be closed.

flush

public static void flush(Flushable... flushables)
Flush all given Flushable instances. Ignore IOException that may be thrown.

Parameters:
flushables - list of Flushable instance to be closed.

chmod

public static final boolean chmod(File file,
                                  String mode)
Change the permissions of a given file by using chmod.

Parameters:
file - a file
mode - a mode
Returns:
true if chmod has been executed successfully

find

public static final Collection<File> find(File directory,
                                          FileFilter fileFilter,
                                          boolean recursive)

getMode

public static final String getMode(File file)
Return the mode for a given file.

Parameters:
file - a file
Returns:
a mode

lastModifiedFileName

public static String lastModifiedFileName(File file)
                                   throws FileUtilsException
For a given file, get the shorter name containing the last modification date.

Parameters:
file - the given file to analyze to get the file name and the last modification date
Returns:
A new archive name which contains the last modification date of the given deployable
Throws:
FileUtilsException - Thrown if the given file cannot be retrieved

calculateChecksum

public static String calculateChecksum(File file,
                                       String algorithm)
                                throws FileUtilsException
For a given file, calculate its checksum using a given algorithm.

Parameters:
file - The file to check
algorithm - The checksum algorithm
Returns:
The checksum of a given file.
Throws:
FileUtilsException - If the checksum cannot be calculated

readFile

public static String readFile(String path,
                              Charset charset)
                       throws IOException
Return the string contained in the given file, by using the given charset to decode it.

Parameters:
path - the file path
charset - the charset to decode the file
Returns:
the string contained in the given file
Throws:
IOException - if some I/O errors occur

readFile

public static String readFile(String path)
                       throws IOException
Return the string contained in the given file, by using the default charset.

Parameters:
path - the file path
Returns:
the string contained in the given file
Throws:
IOException - if some I/O errors occur


Copyright © 2007-2012 OW2 Consortium. All Rights Reserved.