com.jfw.file
Class FileUtils

java.lang.Object
  extended by com.jfw.file.FileUtils

public final class FileUtils
extends java.lang.Object

File utils methods. All methods are static and thread safe.


Field Summary
static int TYPE_ALL
          All files and dirs must be elaborated.
static int TYPE_DIRS
          Only dirs must be elaborated.
static int TYPE_FILES
          Only files must be elaborated.
 
Constructor Summary
FileUtils()
           
 
Method Summary
static void copyFile(java.lang.String source, java.lang.String name, java.lang.String target, java.lang.String newName, boolean isText)
          Copy a file from one directory to an other.
static void createPath(java.lang.String path)
          Create a path.
static boolean delete(java.lang.String path)
          Delete a file or directory
static boolean deleteFile(java.lang.String pathAndName)
          Delete a file.
static boolean deleteFile(java.lang.String path, java.lang.String name)
          Delete a file.
static boolean exists(java.lang.String fileName)
          Check if a file exist.
static java.util.ArrayList getDirElements(java.lang.String directory, int type)
          Get all elements in path, order by name.
static java.util.ArrayList getDirElementsNames(java.util.ArrayList dirElements)
          Get all elements in path, order by name.
static java.util.ArrayList getRootElements()
          Get root directories, order by name.
static java.lang.String getUniqueName()
          Get a randomly unique name.
static boolean isDirectory(java.lang.String path)
          Check if given path is a file or a dir.
static boolean isFile(java.lang.String path)
          Check if given path is a file or a dir.
static void moveFile(java.lang.String source, java.lang.String name, java.lang.String target, java.lang.String newName, boolean isText)
          Move a file from one directory to an other.
static void moveFiles(java.lang.String sourcePath, java.lang.String targetPath, boolean isText)
          Move a file from one directory to an other.
static java.lang.String normalizePath(java.lang.String path)
          Trim the given String and if is need add the separator char at the end.
static void readAndWriteBinaryFile(ReadFile readFile, java.io.OutputStream outputStream, Encryption encryption)
          Read a binary file and write it in an output stream.
static boolean rename(java.lang.String source, java.lang.String destination)
          Rename a file or directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TYPE_ALL

public static final int TYPE_ALL
All files and dirs must be elaborated.

See Also:
Constant Field Values

TYPE_DIRS

public static final int TYPE_DIRS
Only dirs must be elaborated.

See Also:
Constant Field Values

TYPE_FILES

public static final int TYPE_FILES
Only files must be elaborated.

See Also:
Constant Field Values
Constructor Detail

FileUtils

public FileUtils()
Method Detail

isFile

public static boolean isFile(java.lang.String path)
                      throws JFWGenericException
Check if given path is a file or a dir.

Parameters:
path - the path to check.
Returns:
true if is a file, false in other cases.
Throws:
JFWGenericException

isDirectory

public static boolean isDirectory(java.lang.String path)
                           throws JFWGenericException
Check if given path is a file or a dir.

Parameters:
path - the path to check.
Returns:
true if is a file, false in other cases.
Throws:
JFWGenericException

getDirElements

public static java.util.ArrayList getDirElements(java.lang.String directory,
                                                 int type)
                                          throws JFWGenericException
Get all elements in path, order by name.

Parameters:
directory - the path of a directory.
type - one of the values of attributes TYPE_ALL, TYPE_DIRS and TYPE_FILES. Only this type of directory elements is returned.
Returns:
an ArrayList of File with the elements of the directory. In index 0 is saved the parent directory (if type is TYPE_ALL or TYPE_DIRS). In index 0 the directory set in parameter directory (if type is TYPE_ALL or TYPE_DIRS).
Throws:
JFWGenericException

getDirElementsNames

public static java.util.ArrayList getDirElementsNames(java.util.ArrayList dirElements)
                                               throws JFWGenericException
Get all elements in path, order by name.

Parameters:
dirElements - an ArrayList of File returned by getDirElements(java.lang.String, int).
Returns:
ua ArrayList of String with the files names.
Throws:
JFWGenericException

getRootElements

public static java.util.ArrayList getRootElements()
                                           throws JFWGenericException
Get root directories, order by name.

Returns:
an ArrayList of File with the roots elements.
Throws:
JFWGenericException

exists

public static boolean exists(java.lang.String fileName)
Check if a file exist.

Returns:
true if file exist, false in other cases.

createPath

public static void createPath(java.lang.String path)
Create a path. Create all missing directories.

Parameters:
path - the path to create.

copyFile

public static void copyFile(java.lang.String source,
                            java.lang.String name,
                            java.lang.String target,
                            java.lang.String newName,
                            boolean isText)
                     throws JFWGenericException
Copy a file from one directory to an other.

Parameters:
source - source path of file.
name - file name.
target - target path to copy.
newName - name of file in target path. If null the original name is used.
isText - true if is a text file, false if is a binary file.
Throws:
JFWGenericException

moveFiles

public static void moveFiles(java.lang.String sourcePath,
                             java.lang.String targetPath,
                             boolean isText)
                      throws JFWGenericException
Move a file from one directory to an other.

Parameters:
sourcePath - source path and file name of file.
targetPath - target path and file name of file.
isText - true if is a text file, false if is a binary file.
Throws:
JFWGenericException

moveFile

public static void moveFile(java.lang.String source,
                            java.lang.String name,
                            java.lang.String target,
                            java.lang.String newName,
                            boolean isText)
                     throws JFWGenericException
Move a file from one directory to an other.

Parameters:
source - source path of file.
name - file name.
target - target path to copy.
newName - name of file in target path. If null the original name is used.
isText - true if is a text file, false if is a binary file.
Throws:
JFWGenericException

deleteFile

public static boolean deleteFile(java.lang.String path,
                                 java.lang.String name)
Delete a file.

Parameters:
path - path of file.
name - file name.
Returns:
true if no error occur, false in other cases.

deleteFile

public static boolean deleteFile(java.lang.String pathAndName)
Delete a file.

Parameters:
pathAndName - path and name of a file.
Returns:
true if no error occur, false in other cases.

delete

public static boolean delete(java.lang.String path)
Delete a file or directory

Parameters:
path - if this path is a directory then all subdirectories and files will also delete.
Returns:
true if no error occur, false in other cases.

normalizePath

public static java.lang.String normalizePath(java.lang.String path)
Trim the given String and if is need add the separator char at the end.

Parameters:
path - path to change.
Returns:
changed path.

getUniqueName

public static java.lang.String getUniqueName()
Get a randomly unique name.

Returns:
a randomly unique name.

rename

public static boolean rename(java.lang.String source,
                             java.lang.String destination)
Rename a file or directory.

Parameters:
source - the absolute name of the file/directory to rename.
destination - the absolute name of the new renamed file/directory.
Returns:
true in success, false in error.

readAndWriteBinaryFile

public static void readAndWriteBinaryFile(ReadFile readFile,
                                          java.io.OutputStream outputStream,
                                          Encryption encryption)
                                   throws JFWGenericException
Read a binary file and write it in an output stream. The content of the output stream can be encrypted.

Parameters:
readFile - the file to read.
outputStream - the output stream to write.
encryption - an Encryption object. Set to null if you don't need to encrypt the data.
Throws:
JFWGenericException