org.bibalex.daf.handlers.filehandler
Class LocalFileHandler

java.lang.Object
  extended by org.bibalex.daf.handlers.filehandler.LocalFileHandler

public class LocalFileHandler
extends java.lang.Object

Manipulates the file/folder operation over local system

Author:
Mohammed.Abuouda

Field Summary
private  int compareResult
           
 
Constructor Summary
LocalFileHandler()
          Creates a new instance of LocalFileHandler.
 
Method Summary
 int compareFile(java.lang.String sourceFile, java.lang.String destFile)
          Compare the two given files.
 int compareFolder(java.lang.String sourceFolder, java.lang.String destFolder)
          Compares the two given folders.
 boolean copyFile(java.lang.String sourceFile, java.lang.String destFolder)
          Copy file from source to destination with ability to change file name.
private  void copyFolder(java.lang.String sourceFolder, java.lang.String destinationFolder)
          Copies the folder from source to destination.
 boolean copyFolder(java.lang.String sourceParent, java.lang.String folderName, java.lang.String destinationParent, java.lang.String newName, boolean overwrite)
          Copies a folder from source to destination.
 boolean createFile(java.lang.String parentPath, java.lang.String fileName, boolean overwrite)
          Creates a file within the given folder.
 boolean createFolder(java.lang.String parentPath, java.lang.String folderName, boolean overwrite)
          Creates a folder at the specified path.
 boolean createPath(java.lang.String path)
          Creates the complete path.
 boolean deleteFile(java.lang.String parentPath, java.lang.String fileName)
          Deletes the given file.
 boolean deleteFolder(java.lang.String folderPath)
          Delete the files and subfolder recursively.
 boolean folderExists(java.lang.String folderPath)
          Determines whether the given folder exists or not.
private  java.lang.String formatPath(java.lang.String path)
          Format the path according to the current platform.
 java.io.File[] getFiles(java.lang.String path)
          Gets the files within the given folder.
 int getFilesCount(java.lang.String path)
          Gets the count of files within the given folder.
 int getFilesCountRecursively(java.lang.String path)
          Gets the count of files within the given folder recursively.
 int getFilesCountWithExt(java.lang.String path, java.lang.String extension)
          Gets the count of files with the specified extention.
 long getFileSize(java.lang.String filePath)
          Gets the size of the file in bytes.
 java.lang.String[] getFilesNames(java.lang.String path)
          Gets the names of files within the given folder.
 java.lang.String[] getFilesNamesWithExt(java.lang.String path, java.lang.String extension)
          Gets the names of the file with the specified extension within the given folder.
 java.io.File[] getFilesRecursively(java.lang.String path)
          Gets the files within the given folder recursively.
 java.io.File[] getFilesWithExt(java.lang.String path, java.lang.String extension)
          Gets the files with the specified extention.
 long getFolderSize(java.lang.String folderPath)
          Gets the size of the folder, recursively, in bytes.
 java.lang.String[] getSubFolders(java.lang.String path)
          Gets the names of subfolders.
 boolean moveFile(java.lang.String sourceFile, java.lang.String destFolder, boolean overwrite)
          Moves file from source to destination with ability to overwrite an old existed copy.
 boolean moveFolder(java.lang.String sourceFolder, java.lang.String destParent, boolean overwrite)
          Move folder from source to destination with ability to overwrite an old existed copy.
 boolean rename(java.lang.String path, java.lang.String newName, boolean overwrite)
          Rename a file or a folder.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

compareResult

private int compareResult
Constructor Detail

LocalFileHandler

public LocalFileHandler()
Creates a new instance of LocalFileHandler.

Method Detail

moveFile

public boolean moveFile(java.lang.String sourceFile,
                        java.lang.String destFolder,
                        boolean overwrite)
Moves file from source to destination with ability to overwrite an old existed copy.

Parameters:
sourceFile - The path of the file to move including the file name.
destFolder - The folder to move the file to.
overwrite - Whether to overwrite the file at destination or not.
Returns:
True if the file has been moved successfully otherwise false

moveFolder

public boolean moveFolder(java.lang.String sourceFolder,
                          java.lang.String destParent,
                          boolean overwrite)
Move folder from source to destination with ability to overwrite an old existed copy.

Parameters:
sourceFolder - The path of the folder to move including the folder name.
destParent - The destination parent folder to which the folder will be moved.
overwrite - Whether to overwrite the folder at destination or not.
Returns:
True if the folder has been moved, otherwise false.

deleteFolder

public boolean deleteFolder(java.lang.String folderPath)
Delete the files and subfolder recursively.

Parameters:
folderPath - The path of the folder to delete.
Returns:
True if the folder has been deleted otherwise false.

deleteFile

public boolean deleteFile(java.lang.String parentPath,
                          java.lang.String fileName)
Deletes the given file.

Parameters:
parentPath - The path to the folder taht contains the file.
fileName - The name of the file to delete.
Returns:
True when done successfully, otherwise false.

rename

public boolean rename(java.lang.String path,
                      java.lang.String newName,
                      boolean overwrite)
Rename a file or a folder.

Parameters:
path - The path to the folder or the file including the old file/folder name.
newName - The new name of the fie or the folder. The parent path is not included.
overwrite - Whether to overwrite a file or a folder with the same name or not.
Returns:
True if the file or folder has been renamed successfully, otherwise false.

getSubFolders

public java.lang.String[] getSubFolders(java.lang.String path)
Gets the names of subfolders.

Parameters:
path - The path of the folder to get its sub-folders
Returns:
A String[] holding the names of sub-folders. If there is no subfolders an empty array is returned. If the given path doesn't represent an existing folder, null is returned.

getFilesCount

public int getFilesCount(java.lang.String path)
Gets the count of files within the given folder.

Parameters:
path - The path of the folder to count its files.
Returns:
The number of files within the folder, -1 means the folder doesn't exist or an exception occurrred.

getFilesCountRecursively

public int getFilesCountRecursively(java.lang.String path)
Gets the count of files within the given folder recursively.

Parameters:
path - The path of the folder to count its files.
Returns:
The number of files within the folder, -1 means the folder doesn't exist or an exception occurrred.

getFiles

public java.io.File[] getFiles(java.lang.String path)
Gets the files within the given folder.

Parameters:
path - The path of the folder to count its files.
Returns:
The files within the folder. If an exception occurs or the folder doesn't exist, an empty File[] is returned.

getFilesRecursively

public java.io.File[] getFilesRecursively(java.lang.String path)
Gets the files within the given folder recursively.

Parameters:
path - The path of the folder to count its files.
Returns:
The files within the folder. If an exception occurs or the folder doesn't exist, an empty File[] is returned.

getFilesNames

public java.lang.String[] getFilesNames(java.lang.String path)
Gets the names of files within the given folder.

Parameters:
path - The path of the folder to get its files.
Returns:
A String[] holding the names of sub-files. If there is no subfiles an empty array is returned. If the given path doesn't represent an existing folder, null is returned.

getFilesCountWithExt

public int getFilesCountWithExt(java.lang.String path,
                                java.lang.String extension)
Gets the count of files with the specified extention.

Parameters:
path - The folder path to count its files.
extension - The extension of the files. The extension is the last 3 letters of the file name without the dot '.' .
Returns:
Files count filtered on extension, -1 means exception.

getFilesWithExt

public java.io.File[] getFilesWithExt(java.lang.String path,
                                      java.lang.String extension)
Gets the files with the specified extention.

Parameters:
path - The folder path to get its files.
extension - The extension of the files. The extension is the last 3 letters of the file name without the dot '.' .
Returns:
Files filtered on extension, null means exception.

getFilesNamesWithExt

public java.lang.String[] getFilesNamesWithExt(java.lang.String path,
                                               java.lang.String extension)
Gets the names of the file with the specified extension within the given folder.

Parameters:
path - The path fo the foldders to get the names of the files within.
extension - The extension of the files to get their names. The extension is the last 3 letters of the file name without the dot '.' .
Returns:
A String[] holding the names of sub-files. If there is no subfiles an empty array is returned. If the given path doesn't represent an existing folder, null is returned.

formatPath

private java.lang.String formatPath(java.lang.String path)
Format the path according to the current platform.

Parameters:
path - The path to reformat.
Returns:
The path formatted according to the current platform.

createFolder

public boolean createFolder(java.lang.String parentPath,
                            java.lang.String folderName,
                            boolean overwrite)
Creates a folder at the specified path.

Parameters:
parentPath - The path of the folder at which the folder will be created.
folderName - The name of the fooder to create.
overwrite - Whether to overwrite a folder with the same name at the parent or not.
Returns:
True if the folder has been crated, otherwise false.

createFile

public boolean createFile(java.lang.String parentPath,
                          java.lang.String fileName,
                          boolean overwrite)
Creates a file within the given folder.

Parameters:
parentPath - The parent folder to create the file in.
fileName - The name of the file to create.
overwrite - Whether to overwrite a file with the same name at the parent or not
Returns:
True if the file has been created, otherwise false.

copyFolder

public boolean copyFolder(java.lang.String sourceParent,
                          java.lang.String folderName,
                          java.lang.String destinationParent,
                          java.lang.String newName,
                          boolean overwrite)
Copies a folder from source to destination. Copying is done in two steps: first the folders are copied then compared against the source.

Parameters:
sourceParent - The source parent folder without the name of the folder.
folderName - The name of the folder.
destinationParent - The destination parent folder without the name of the folder.
newName - The new name of the folder.
overwrite - Whether to overwrite a folder with the same name at the destination or not.
Returns:
True if the folder has been copied otherwise false.

folderExists

public boolean folderExists(java.lang.String folderPath)
Determines whether the given folder exists or not.

Parameters:
folderPath - The path of the folder.
Returns:
True if the folder exists.

copyFolder

private void copyFolder(java.lang.String sourceFolder,
                        java.lang.String destinationFolder)
Copies the folder from source to destination. This method is called recursively to copy folders, subfolders, ....

Parameters:
sourceFolder - The source folder.
destinationFolder - The destination folder.

compareFolder

public int compareFolder(java.lang.String sourceFolder,
                         java.lang.String destFolder)
Compares the two given folders.

Parameters:
sourceFolder - The path to the source folder.
destFolder - The path of the destination folder.
Returns:
0 if both folders are the same, 1 if there are differences.

compareFile

public int compareFile(java.lang.String sourceFile,
                       java.lang.String destFile)
Compare the two given files.

Parameters:
sourceFile - The source file.
destFile - The destination file.
Returns:
0 if both files are the same, 1 if there are differences.

copyFile

public boolean copyFile(java.lang.String sourceFile,
                        java.lang.String destFolder)
Copy file from source to destination with ability to change file name.

Parameters:
sourceFile - The path to the source file.
destFolder - The path of the destination folder.
Returns:
True if the file has been copied, otherwise false.

getFolderSize

public long getFolderSize(java.lang.String folderPath)
Gets the size of the folder, recursively, in bytes.

Parameters:
folderPath - The path of the folder.
Returns:
The size of the folder in bytes. If the folder doesn't exists or the size couldn't be obtained -1 is returned.

getFileSize

public long getFileSize(java.lang.String filePath)
Gets the size of the file in bytes.

Parameters:
filePath - The path of the file.
Returns:
The size of the file in bytes. If the file doesn't exists or the size couldn't be obtained -1 is returned.

createPath

public boolean createPath(java.lang.String path)
Creates the complete path. This method creates the necessary folders to create the whole path. For example: If the given path is "D:\User Data\User Name\My job\Scannig" and only "D:\User Data\User Name" exists, The folders "My job\Scannig" are created.

Parameters:
path - The path to create
Returns:
True if the path has been created or already exists otherwise false.