com.gridsystems.utils
Class FileUtils

java.lang.Object
  extended by com.gridsystems.utils.FileUtils

public final class FileUtils
extends java.lang.Object

General File Utilities.

Version:
1.0
Author:
Xmas

Nested Class Summary
static interface FileUtils.UnzipListener
          Listener used to show information of unzipping process.
 
Field Summary
static int FOUR_KB_IN_BYTES
          Four KB in bytes.
static int ONE_KB_IN_BYTES
          Oner KB in bytes.
static int THIRTY_TWO_KB_IN_BYTES
          Four KB in bytes.
 
Method Summary
static java.lang.Integer chmod(java.lang.String perms, java.io.File f, boolean recursive)
          Equivalent to Unix chmod command.
static void close(java.lang.Object obj)
          Closes any object having a close() method without exceptions.
static void copy(java.io.File ffrom, java.io.File fto)
          Copy a file from ffrom to fto.
static boolean copyDir(java.io.File srcDir, java.io.File dstDir)
          Copia el contenido del directorio src_dir dentro del directorio dst_dir Si dst_dir no existe entonces lo crea.
static boolean copyDirNative(java.io.File srcDir, java.io.File dstDir)
          Recursively copies the contents of srcDir into dstDir using external native commands.
static boolean copyNative(java.io.File srcFile, java.io.File dstFile)
          Copies the file src_file into dst_file using native (O.S.) calls.
static void copyStream(java.io.OutputStream dst, java.io.InputStream src)
          Copies a stream into another.
static void createFile(java.io.InputStream srcIn, java.io.File dstFile)
          Copies the content of the input stream srcIn into the file dstFile.
static void fileToStream(java.io.File file, java.io.OutputStream dest)
          Writes the contents of a file to an stream.
static long getDirectorySize(java.io.File f, java.io.FileFilter filter)
          Finds out the size of all files within the specified directory, including subdirectories.
static java.util.Properties loadPropertiesFromFile(java.io.File f)
          Read properties from a file.
static java.util.Properties mergeProperties(java.util.Properties base, java.util.Properties olds)
          Merge 2 properties into one.
static void move(java.io.File src, java.io.File dst)
          Moves the contents of srcFile into dstFile.
static boolean moveNative(java.io.File fsrc, java.io.File fdst)
          Moves a source file to a destination by calling a native command.
static java.lang.String[] readListFile(java.io.File f)
          Reads a text file containing a string item by line, and returns an array containing those lines not beginning by a '#' character.
static java.lang.String readText(java.io.File f)
          Reads the content of the text file and returns it as a String.
static boolean rmtree(java.io.File f)
          If f is a file then deletes it; if it is a directory then deletes its files and subdirectories, and after that, tries to delete it.
static boolean rmtreeNative(java.io.File dir)
          If parameter file is a file then delete this file.
static void savePropertiesToFile(java.io.File f, java.util.Properties props, java.lang.String header)
          Stores properties to a file.
static void unzip(java.io.File dir, java.io.File f, boolean stripPrefix)
          Uncompresses the specified file into the specified directory.
static void unzip(java.io.File dir, java.io.File f, boolean stripPrefix, FileUtils.UnzipListener unziplistener)
          Uncompresses the specified file into the specified directory.
static void unzip(java.io.File dir, java.io.File f, boolean stripPrefix, FileUtils.UnzipListener unziplistener, java.io.FileFilter filefilter)
          Uncompresses the specified file into the specified directory.
static void unzip(java.io.File dir, java.io.File f, java.io.FileFilter filter)
          Uncompresses the specified file into the specified directory filtering by filter.
static void unzip(java.io.File dir, java.io.File f, FileUtils.UnzipListener unziplistener)
          Uncompresses the specified file into the specified directory.
static void unzip(java.io.InputStream is, java.io.File dir)
          Uncompresses the specified stream into the specified directory.
static void writeText(java.io.File f, java.lang.String text)
          Writes text into a file.
static void zip(java.io.File dst, java.io.File base, java.io.File src, java.io.FileFilter filter)
          Crea un archivo zip del contenido de la raiz de cierto directorio.
static void zip(java.io.File zip, java.io.File filetozip, java.io.FileFilter filter)
          Crea un archivo zip del contenido de la raiz de cierto directorio.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ONE_KB_IN_BYTES

public static final int ONE_KB_IN_BYTES
Oner KB in bytes.

See Also:
Constant Field Values

FOUR_KB_IN_BYTES

public static final int FOUR_KB_IN_BYTES
Four KB in bytes.

See Also:
Constant Field Values

THIRTY_TWO_KB_IN_BYTES

public static final int THIRTY_TWO_KB_IN_BYTES
Four KB in bytes.

See Also:
Constant Field Values
Method Detail

close

public static void close(java.lang.Object obj)
Closes any object having a close() method without exceptions.

Parameters:
obj - The object to close

savePropertiesToFile

public static void savePropertiesToFile(java.io.File f,
                                        java.util.Properties props,
                                        java.lang.String header)
                                 throws java.io.IOException
Stores properties to a file.

Parameters:
f - The destination file
props - The source properties
header - The header line text
Throws:
java.io.IOException - If an error occurs

loadPropertiesFromFile

public static java.util.Properties loadPropertiesFromFile(java.io.File f)
                                                   throws java.io.IOException
Read properties from a file.

Parameters:
f - The file to read
Returns:
The read properties
Throws:
java.io.IOException - If an error occurs

mergeProperties

public static java.util.Properties mergeProperties(java.util.Properties base,
                                                   java.util.Properties olds)
Merge 2 properties into one.

Parameters:
base - the first Properties object
olds - the second Properties object
Returns:
a new Properties object

readText

public static java.lang.String readText(java.io.File f)
                                 throws java.io.IOException
Reads the content of the text file and returns it as a String.

Parameters:
f - the file to read from
Returns:
the contents of the file
Throws:
java.io.IOException - if error

readListFile

public static java.lang.String[] readListFile(java.io.File f)
                                       throws java.io.IOException
Reads a text file containing a string item by line, and returns an array containing those lines not beginning by a '#' character.

Parameters:
f - The file to read
Returns:
The lines of the file
Throws:
java.io.IOException - if error

writeText

public static void writeText(java.io.File f,
                             java.lang.String text)
                      throws java.io.IOException
Writes text into a file.

Parameters:
f - the file to write to
text - the text to be written
Throws:
java.io.IOException - if error

zip

public static void zip(java.io.File zip,
                       java.io.File filetozip,
                       java.io.FileFilter filter)
                throws java.lang.Exception
Crea un archivo zip del contenido de la raiz de cierto directorio.

Parameters:
zip - Archivo donde guardar el directorio comprimido
filetozip - archivo de directorio a comprimir
filter - filtro a aplicar para la selecciona de archivos a comprimir
Throws:
java.lang.Exception - if error

zip

public static void zip(java.io.File dst,
                       java.io.File base,
                       java.io.File src,
                       java.io.FileFilter filter)
                throws java.io.IOException
Crea un archivo zip del contenido de la raiz de cierto directorio.

Parameters:
dst - Archivo donde guardar el directorio comprimido
base - Directorio base a partir del cual apareceran las rutas en los archivos
src - archivo de directorio a comprimir
filter - filtro a aplicar para la selecciona de archivos a comprimir
Throws:
java.io.IOException - if error

unzip

public static void unzip(java.io.File dir,
                         java.io.File f,
                         boolean stripPrefix)
                  throws java.io.IOException
Uncompresses the specified file into the specified directory.

Parameters:
dir - The directory into which to unzip
f - The file to unzip
stripPrefix - Flag that controls if the path prefix is stripped from entries
Throws:
java.io.IOException - If an error occurs

unzip

public static void unzip(java.io.File dir,
                         java.io.File f,
                         FileUtils.UnzipListener unziplistener)
                  throws java.io.IOException
Uncompresses the specified file into the specified directory.

Parameters:
dir - The directory into which to unzip
f - The file to unzip
unziplistener - Listenr for know status of unzipping
Throws:
java.io.IOException - If an error occurs

unzip

public static void unzip(java.io.File dir,
                         java.io.File f,
                         java.io.FileFilter filter)
                  throws java.io.IOException
Uncompresses the specified file into the specified directory filtering by filter.

Parameters:
dir - The directory into which to unzip
f - The file to unzip
filter - Filter files to extract (Only files, not directories)
Throws:
java.io.IOException - If an error occurs

unzip

public static void unzip(java.io.File dir,
                         java.io.File f,
                         boolean stripPrefix,
                         FileUtils.UnzipListener unziplistener)
                  throws java.io.IOException
Uncompresses the specified file into the specified directory.

Parameters:
dir - The directory into which to unzip
f - The file to unzip
stripPrefix - Flag that controls if the path prefix is stripped from entries
unziplistener - Listenr for know status of unzipping
Throws:
java.io.IOException - If an error occurs

unzip

public static void unzip(java.io.File dir,
                         java.io.File f,
                         boolean stripPrefix,
                         FileUtils.UnzipListener unziplistener,
                         java.io.FileFilter filefilter)
                  throws java.io.IOException
Uncompresses the specified file into the specified directory.

Parameters:
dir - The directory into which to unzip
f - The file to unzip
stripPrefix - Flag that controls if the path prefix is stripped from entries
unziplistener - Listenr for know status of unzipping
filefilter - Filter files to extract (Only files, not directories)
Throws:
java.io.IOException - If an error occurs

unzip

public static void unzip(java.io.InputStream is,
                         java.io.File dir)
                  throws java.lang.Exception
Uncompresses the specified stream into the specified directory.

The stream contents is supposed to be a zip file.

Parameters:
is - The stream for reading the zip
dir - The directory into which to unzip
Throws:
java.lang.Exception - if error

rmtree

public static boolean rmtree(java.io.File f)
If f is a file then deletes it; if it is a directory then deletes its files and subdirectories, and after that, tries to delete it.

Parameters:
f - the file or directory to remove
Returns:
true if successful, false otherwise

rmtreeNative

public static boolean rmtreeNative(java.io.File dir)
If parameter file is a file then delete this file. If file is a directory then delete yours subdirectories and files.

Parameters:
dir - the directory to remove
Returns:
true if successful, false otherwise

copyStream

public static void copyStream(java.io.OutputStream dst,
                              java.io.InputStream src)
                       throws java.io.IOException
Copies a stream into another.

Parameters:
dst - the destination stream
src - the source stream
Throws:
java.io.IOException - if error

createFile

public static void createFile(java.io.InputStream srcIn,
                              java.io.File dstFile)
                       throws java.io.IOException
Copies the content of the input stream srcIn into the file dstFile.

Parameters:
srcIn - source input stream for the copy operation
dstFile - destination file for the copy operation
Throws:
java.io.IOException - if error

copyDirNative

public static boolean copyDirNative(java.io.File srcDir,
                                    java.io.File dstDir)
                             throws java.lang.Exception

Recursively copies the contents of srcDir into dstDir using external native commands.

If dstDir does not exists, it is created.

Parameters:
srcDir - Source directory
dstDir - Destination directory
Returns:
true if the operation is successful; false otherwise
Throws:
java.lang.Exception - If the native command could not be executed

copyNative

public static boolean copyNative(java.io.File srcFile,
                                 java.io.File dstFile)
                          throws java.io.IOException
Copies the file src_file into dst_file using native (O.S.) calls. Creates dst_dir if it does not exist.

Parameters:
srcFile - source file for the copy operation
dstFile - destination file for the copy operation
Returns:
true if successful, false otherwise
Throws:
java.io.IOException - If an error occurs

copyDir

public static boolean copyDir(java.io.File srcDir,
                              java.io.File dstDir)
                       throws java.io.IOException
Copia el contenido del directorio src_dir dentro del directorio dst_dir Si dst_dir no existe entonces lo crea.

Parameters:
srcDir - fuente
dstDir - destino
Returns:
true if successful, false otherwise
Throws:
java.io.IOException - if error

copy

public static void copy(java.io.File ffrom,
                        java.io.File fto)
                 throws java.io.IOException
Copy a file from ffrom to fto.

Parameters:
ffrom - the source file
fto - the dest file
Throws:
java.io.IOException - if error

move

public static void move(java.io.File src,
                        java.io.File dst)
                 throws java.io.IOException
Moves the contents of srcFile into dstFile.

Parameters:
src - source file for the move operation
dst - destination file for the move operation
Throws:
java.io.IOException - if error

moveNative

public static boolean moveNative(java.io.File fsrc,
                                 java.io.File fdst)
Moves a source file to a destination by calling a native command. Features of this move function: - If the destination directory exists then moves the file within the specified directory. - If the destination directory does not exist then rename the original directory/file to the one of destiny.

Parameters:
fsrc - The source path
fdst - The destination path
Returns:
true if successful, false otherwise

getDirectorySize

public static long getDirectorySize(java.io.File f,
                                    java.io.FileFilter filter)
Finds out the size of all files within the specified directory, including subdirectories. Only files accepted by the filter are included in the result.

Parameters:
f - the file or directory
filter - the filter for the contents of the directory
Returns:
the number of files in the directory

chmod

public static java.lang.Integer chmod(java.lang.String perms,
                                      java.io.File f,
                                      boolean recursive)
Equivalent to Unix chmod command.

Parameters:
perms - The permissions to set. It can be in the form "u+x" or in octal
f - The file to set permissions on
recursive - recursive behaviour
Returns:
The exit code of the chmod execution

fileToStream

public static void fileToStream(java.io.File file,
                                java.io.OutputStream dest)
                         throws java.io.IOException
Writes the contents of a file to an stream.

Parameters:
file - the File that is read.
dest - the OutputStream where the contents of file are written.
Throws:
java.io.IOException - if error


Copyright © 2007-2008 Grid Systems, S.A.. All Rights Reserved.