com.norconex.commons.lang.io
Class FileUtil

java.lang.Object
  extended by com.norconex.commons.lang.io.FileUtil

public final class FileUtil
extends Object

Utility methods when dealing with files and directories.

Author:
Pascal Essiembre

Method Summary
static File createDateDirs(File parentDir)
          Creates (if not already existing) a series of directories reflecting the current date, up to the day unit, under a given parent directory.
static File createDateDirs(File parentDir, Date date)
          Creates (if not already existing) a series of directories reflecting a date, up to the day unit, under a given parent directory.
static File createDateTimeDirs(File parentDir)
          Creates (if not already existing) a series of directories reflecting the current date and time, up to the seconds, under a given parent directory.
static File createDateTimeDirs(File parentDir, Date dateTime)
          Creates (if not already existing) a series of directories reflecting a date and time, up to the seconds, under a given parent directory.
static File createDirsForFile(File file)
          Create all parent directories for a file if they do not exists.
static int deleteEmptyDirs(File parentDir)
          Deletes all directories that are empty from a given parent directory.
static void deleteFile(File file)
          Deletes a file or a directory recursively.
static String[] head(File file, int numberOfLinesToRead)
          Returns the specified number of lines starting from the beginning of a text file.
static String[] head(File file, String encoding, int numberOfLinesToRead)
          Returns the specified number of lines starting from the beginning of a text file, using the given encoding.
static String[] head(File file, String encoding, int numberOfLinesToRead, boolean stripBlankLines)
          Returns the specified number of lines starting from the beginning of a text file, using the given encoding.
static String[] head(File file, String encoding, int numberOfLinesToRead, boolean stripBlankLines, IInputStreamFilter filter)
          Returns the specified number of lines starting from the beginning of a text file, using the given encoding.
static void moveFile(File sourceFile, File targetFile)
          Moves a file to a new file location.
static void moveFileToDir(File sourceFile, File targetDir)
          Moves a file to a directory.
static String[] tail(File file, int numberOfLinesToRead)
          Returns the specified number of lines starting from the end of a text file.
static String[] tail(File file, String encoding, int numberOfLinesToRead)
          Returns the specified number of lines starting from the end of a text file.
static String[] tail(File file, String encoding, int numberOfLinesToRead, boolean stripBlankLines)
          Returns the specified number of lines starting from the end of a text file.
static String[] tail(File file, String encoding, int numberOfLinesToRead, boolean stripBlankLines, IInputStreamFilter filter)
          Returns the specified number of lines starting from the end of a text file.
static void visitAllDirs(File dir, IFileVisitor visitor)
          Visits only directories under a directory.
static void visitAllDirsAndFiles(File dir, IFileVisitor visitor)
          Visits all files and directories under a directory.
static void visitAllDirsAndFiles(File dir, IFileVisitor visitor, FileFilter filter)
          Visits all files and directories under a directory.
static void visitAllFiles(File dir, IFileVisitor visitor)
          Visits all files (and only files) under a directory, including sub-directories.
static void visitAllFiles(File dir, IFileVisitor visitor, FileFilter filter)
          Visits all files (and only files) under a directory, including sub-directories.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

moveFileToDir

public static void moveFileToDir(File sourceFile,
                                 File targetDir)
                          throws IOException
Moves a file to a directory. Like moveFile(File, File):

Parameters:
sourceFile - source file to move
targetDir - target destination
Throws:
IOException - cannot move file.

moveFile

public static void moveFile(File sourceFile,
                            File targetFile)
                     throws IOException
Moves a file to a new file location. This method is different from the File.renameTo(File) method in such that:

Parameters:
sourceFile - source file to move
targetFile - target destination
Throws:
IOException - cannot move file.

deleteFile

public static void deleteFile(File file)
                       throws IOException
Deletes a file or a directory recursively. This method does:

Parameters:
file - file or directory to delete
Throws:
IOException - cannot delete file.

deleteEmptyDirs

public static int deleteEmptyDirs(File parentDir)
Deletes all directories that are empty from a given parent directory.

Parameters:
parentDir - the directory where to start looking for empty directories
Returns:
the number of deleted directories

createDirsForFile

public static File createDirsForFile(File file)
                              throws IOException
Create all parent directories for a file if they do not exists. If they exist already, this method does nothing. This method assumes the last segment is a file or will be a file.

Parameters:
file - the file to create parent directories for
Returns:
The newly created parent directory
Throws:
IOException - if somethign went wrong creating the parent directories

visitAllDirsAndFiles

public static void visitAllDirsAndFiles(File dir,
                                        IFileVisitor visitor)
Visits all files and directories under a directory.

Parameters:
dir - the directory
visitor - the visitor

visitAllDirsAndFiles

public static void visitAllDirsAndFiles(File dir,
                                        IFileVisitor visitor,
                                        FileFilter filter)
Visits all files and directories under a directory.

Parameters:
dir - the directory
visitor - the visitor
filter - an optional filter to restrict the files being visited

visitAllDirs

public static void visitAllDirs(File dir,
                                IFileVisitor visitor)
Visits only directories under a directory.

Parameters:
dir - the directory
visitor - the visitor

visitAllFiles

public static void visitAllFiles(File dir,
                                 IFileVisitor visitor)
Visits all files (and only files) under a directory, including sub-directories.

Parameters:
dir - the directory
visitor - the visitor

visitAllFiles

public static void visitAllFiles(File dir,
                                 IFileVisitor visitor,
                                 FileFilter filter)
Visits all files (and only files) under a directory, including sub-directories.

Parameters:
dir - the directory
visitor - the visitor
filter - an optional filter to restrict the files being visited

head

public static String[] head(File file,
                            int numberOfLinesToRead)
                     throws IOException
Returns the specified number of lines starting from the beginning of a text file.

Parameters:
file - the file to read lines from
numberOfLinesToRead - the number of lines to read
Returns:
array of file lines
Throws:
IOException

head

public static String[] head(File file,
                            String encoding,
                            int numberOfLinesToRead)
                     throws IOException
Returns the specified number of lines starting from the beginning of a text file, using the given encoding.

Parameters:
file - the file to read lines from
encoding - the file encoding
numberOfLinesToRead - the number of lines to read
Returns:
array of file lines
Throws:
IOException

head

public static String[] head(File file,
                            String encoding,
                            int numberOfLinesToRead,
                            boolean stripBlankLines)
                     throws IOException
Returns the specified number of lines starting from the beginning of a text file, using the given encoding.

Parameters:
file - the file to read lines from
encoding - the file encoding
numberOfLinesToRead - the number of lines to read
stripBlankLines - whether to return blank lines or not
Returns:
array of file lines
Throws:
IOException

head

public static String[] head(File file,
                            String encoding,
                            int numberOfLinesToRead,
                            boolean stripBlankLines,
                            IInputStreamFilter filter)
                     throws IOException
Returns the specified number of lines starting from the beginning of a text file, using the given encoding.

Parameters:
file - the file to read lines from
encoding - the file encoding
numberOfLinesToRead - the number of lines to read
stripBlankLines - whether to return blank lines or not
filter - InputStream filter
Returns:
array of file lines
Throws:
IOException

tail

public static String[] tail(File file,
                            int numberOfLinesToRead)
                     throws IOException
Returns the specified number of lines starting from the end of a text file.

Parameters:
file - the file to read lines from
numberOfLinesToRead - the number of lines to read
Returns:
array of file lines
Throws:
IOException

tail

public static String[] tail(File file,
                            String encoding,
                            int numberOfLinesToRead)
                     throws IOException
Returns the specified number of lines starting from the end of a text file.

Parameters:
file - the file to read lines from
encoding - the file encoding
numberOfLinesToRead - the number of lines to read
Returns:
array of file lines
Throws:
IOException

tail

public static String[] tail(File file,
                            String encoding,
                            int numberOfLinesToRead,
                            boolean stripBlankLines)
                     throws IOException
Returns the specified number of lines starting from the end of a text file.

Parameters:
file - the file to read lines from
encoding - the file encoding
numberOfLinesToRead - the number of lines to read
stripBlankLines - whether to return blank lines or not
Returns:
array of file lines
Throws:
IOException

tail

public static String[] tail(File file,
                            String encoding,
                            int numberOfLinesToRead,
                            boolean stripBlankLines,
                            IInputStreamFilter filter)
                     throws IOException
Returns the specified number of lines starting from the end of a text file.

Parameters:
file - the file to read lines from
encoding - the file encoding
numberOfLinesToRead - the number of lines to read
stripBlankLines - whether to return blank lines or not
filter - InputStream filter
Returns:
array of file lines
Throws:
IOException

createDateDirs

public static File createDateDirs(File parentDir)
                           throws IOException
Creates (if not already existing) a series of directories reflecting the current date, up to the day unit, under a given parent directory. For example, a date of 2000-12-31 will create the following directory structure: /<parentDir>/2000/12/31/

Parameters:
parentDir - the parent directory where to create date directories
Returns:
the directory representing the full path created
Throws:
IOException - if the parent directory is not valid

createDateDirs

public static File createDateDirs(File parentDir,
                                  Date date)
                           throws IOException
Creates (if not already existing) a series of directories reflecting a date, up to the day unit, under a given parent directory. For example, a date of 2000-12-31 will create the following directory structure: /<parentDir>/2000/12/31/

Parameters:
parentDir - the parent directory where to create date directories
date - the date to create directories from
Returns:
the directory representing the full path created
Throws:
IOException - if the parent directory is not valid

createDateTimeDirs

public static File createDateTimeDirs(File parentDir)
                               throws IOException
Creates (if not already existing) a series of directories reflecting the current date and time, up to the seconds, under a given parent directory. For example, a date of 2000-12-31T13:34:12 will create the following directory structure: /<parentDir>/2000/12/31/13/34/12/

Parameters:
parentDir - the parent directory where to create date directories
Returns:
the directory representing the full path created
Throws:
IOException - if the parent directory is not valid

createDateTimeDirs

public static File createDateTimeDirs(File parentDir,
                                      Date dateTime)
                               throws IOException
Creates (if not already existing) a series of directories reflecting a date and time, up to the seconds, under a given parent directory. For example, a date of 2000-12-31T13:34:12 will create the following directory structure: /<parentDir>/2000/12/31/13/34/12/

Parameters:
parentDir - the parent directory where to create date directories
dateTime - the date to create directories from
Returns:
the directory representing the full path created
Throws:
IOException - if the parent directory is not valid


Copyright © 2008-2013 Norconex Inc.. All Rights Reserved.