com.senn.magic.util
Class FileUtils

java.lang.Object
  extended by com.senn.magic.util.FileUtils

public final class FileUtils
extends Object

This class contains useful methods for file modifications.

Since:
1.0
Author:
Bart Thierens
Last modification: 06/06/2010

Method Summary
static int childrenCount(File file)
          Counts how many children are in the folder.
static void copy(File srcFile, File destFile)
          Copy file (with content) from source to destination
static boolean delete(File toDelete)
          Delete a file from the file system
static boolean delete(String path)
          Delete a file from the file system
static boolean deleteDir(File dir)
          Deletes a folder completely including all his children.
static int directoryCount(File file)
          Counts how many actual folders are in the folder.
static int fileCount(File file)
          Counts how many actual files are in the folder.
static byte[] getBytesFromFile(File file)
          Returns the file (File object) as an array of bytes.
static String getDirectoryFromFileName(String filename)
          Return name of the directory from a provided File's name.
static String getExtension(File file)
          Get the extension of a File.
static String getExtension(String path)
          Get the extension of a File for the specified path
static String getFileName(String path)
          Return name of file without its extension.
static List<File> getFilesInFolder(File folder, boolean andInSubfolders, boolean includeSubFoldersInList)
          Return the list of files (File objects) in the specified folder (File object).
static List<File> getFilesInFolder(String folderPath, boolean andInSubfolders, boolean includeSubFoldersInList)
          Return the list of files (File objects) in the specified folder path.
static List<File> getFilesInFolderByExtension(File folder, String extension, boolean andInSubfolders, boolean includeSubFoldersInList)
          Return the list of files (File objects), with the specified extension, in the specified folder
static List<File> getFilesInFolderByExtension(String folderPath, String extension, boolean andInSubfolders, boolean includeSubFoldersInList)
          Return the list of files (File objects), with the specified extension, in the specified folder path.
static String getParentFolderName(File file)
          Returns the name of the containing folder (e.g.: file is located in C:/headFolder/001/fileName.txt, then containing folder = 001)
static boolean hasExtension(String extension, File file)
          Check to see if the file is of type extension (ignoring case sensitivity).
static void move(File srcFile, File destFile)
          Move a file.
static String rename(File file, String filePath)
          Rename a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getFilesInFolder

public static List<File> getFilesInFolder(String folderPath,
                                          boolean andInSubfolders,
                                          boolean includeSubFoldersInList)
Return the list of files (File objects) in the specified folder path.

Parameters:
folderPath - path of the folder
andInSubfolders - if true, include the files in the sub folders of the folderPath
includeSubFoldersInList - if true, include the sub folders in the list
Returns:
List - list of files

getFilesInFolder

public static List<File> getFilesInFolder(File folder,
                                          boolean andInSubfolders,
                                          boolean includeSubFoldersInList)
Return the list of files (File objects) in the specified folder (File object).

Parameters:
folder - path of the folder
andInSubfolders - if true, include the files in the sub folders of the folderPath
includeSubFoldersInList - if true, include the sub folders in the list
Returns:
List - list of files

getFilesInFolderByExtension

public static List<File> getFilesInFolderByExtension(File folder,
                                                     String extension,
                                                     boolean andInSubfolders,
                                                     boolean includeSubFoldersInList)
Return the list of files (File objects), with the specified extension, in the specified folder

Parameters:
folder - folder to scan
extension - extension to look for
andInSubfolders - if true, include the files in the sub folders of the folder path
includeSubFoldersInList - if true, include the sub folders in the list
Returns:
List - list of files

getFilesInFolderByExtension

public static List<File> getFilesInFolderByExtension(String folderPath,
                                                     String extension,
                                                     boolean andInSubfolders,
                                                     boolean includeSubFoldersInList)
Return the list of files (File objects), with the specified extension, in the specified folder path.

Parameters:
folderPath - folder to scan
extension - extension to look for
andInSubfolders - if true, include the files in the sub folders of the folder path
includeSubFoldersInList - if true, include the sub folders in the list
Returns:
List - list of files

getBytesFromFile

public static byte[] getBytesFromFile(File file)
                               throws IOException
Returns the file (File object) as an array of bytes.

Parameters:
file - file to convert
Returns:
byte[] - an array of bytes
Throws:
IOException

copy

public static void copy(File srcFile,
                        File destFile)
                 throws IOException
Copy file (with content) from source to destination

Parameters:
srcFile - file to copy
destFile - destination folder
Throws:
IOException

getExtension

public static String getExtension(File file)
Get the extension of a File.

Parameters:
file - file to get the extension from
Returns:
String - the extension of a file

getExtension

public static String getExtension(String path)
Get the extension of a File for the specified path

Parameters:
path - path of the file to get the extension from
Returns:
String - the extension of a file

hasExtension

public static boolean hasExtension(String extension,
                                   File file)
Check to see if the file is of type extension (ignoring case sensitivity).

Parameters:
extension - extension to check
file - file to check
Returns:
boolean - true if file has the provided extension, false if it doesn't

getFileName

public static String getFileName(String path)
Return name of file without its extension.

Parameters:
path - path of the file
Returns:
String - name of the file

getDirectoryFromFileName

public static String getDirectoryFromFileName(String filename)
Return name of the directory from a provided File's name.

Parameters:
filename - name of the file
Returns:
String - name of the directory

getParentFolderName

public static String getParentFolderName(File file)
Returns the name of the containing folder (e.g.: file is located in C:/headFolder/001/fileName.txt, then containing folder = 001)

Parameters:
file - file to get the parent folder from
Returns:
String - name of the containing folder

rename

public static String rename(File file,
                            String filePath)
Rename a file.

Parameters:
file - old file
filePath - new file with path
Returns:
String - the new path of the file

move

public static void move(File srcFile,
                        File destFile)
                 throws IOException
Move a file. So literally: move source file to destination file and delete source file afterwards

Parameters:
srcFile - source file
destFile - destination file
Throws:
IOException

delete

public static boolean delete(File toDelete)
                      throws IOException
Delete a file from the file system

Parameters:
toDelete - file to delete
Returns:
boolean true if and only if it succeeded, else false
Throws:
IOException

delete

public static boolean delete(String path)
                      throws IOException
Delete a file from the file system

Parameters:
path - path of the file to delete
Returns:
boolean true if and only if it succeeded, else false
Throws:
IOException

childrenCount

public static int childrenCount(File file)
Counts how many children are in the folder.

Parameters:
file - file representing a folder
Returns:
int - the children count or -1 if the file is not a folder

fileCount

public static int fileCount(File file)
Counts how many actual files are in the folder.

Parameters:
file - file representing a folder
Returns:
int - the file count or -1 if the file is not a folder

directoryCount

public static int directoryCount(File file)
Counts how many actual folders are in the folder.

Parameters:
file - file representing a folder
Returns:
int - the folder count or -1 if the file is not a folder

deleteDir

public static boolean deleteDir(File dir)
Deletes a folder completely including all his children.

Parameters:
dir - directory to delete
Returns:
boolean - true if everything was deleted successfully, false if something went wrong