nl.knowledgeplaza.util
Class FileUtil

java.lang.Object
  extended by nl.knowledgeplaza.util.FileUtil

public class FileUtil
extends Object

This class provides basic facilities for manipulating files and file paths.

Path-related methods

Methods exist to retrieve the components of a typical file path. For example /www/hosted/mysite/index.html, can be broken into:

There are also methods to concatenate two paths, resolve a path relative to a File and normalize(java.lang.String) a path.

File-related methods

There are methods to create a File from a URL, copy a File to a directory, copy a File to another File, copy a URL's contents to a File, as well as methods to delete and clean a directory.

Version:
$Revision: 1.6 $
Author:
$Author: toeukpap $

Nested Class Summary
static class FileUtil.TextfileIterator
          A class for iterating over a file
 
Method Summary
static void appendFile(File source, File destination)
          conveniance
static void appendToFile(File file, String text)
          Append text to file
static void appendToFile(String filename, String text)
          Append text to file
static String catPath(String lookupPath, String path)
          Will concatenate 2 paths.
static void cleanDirectory(File directory)
          Clean a directory without deleting it.
static void cleanDirectory(String directory)
          Clean a directory without deleting it.
static boolean contentEquals(File file1, File file2)
          Compare the contents of two files to determine if they are equal or not.
static void copyFile(File source, File destination)
          conveniance
static void copyFile(File source, File destination, boolean append)
          Copy file from source to destination.
static void copyFileToDirectory(File source, File destinationDirectory)
          Copy file from source to destination.
static void copyFileToDirectory(String source, String destinationDirectory)
          Copy file from source to destination.
static void copyURLToFile(URL source, File destination)
          Copies bytes from the URL source to a file destination.
static File createTempDir()
          Create temp dir at the default location
static File createTempDir(String prefix, String suffix)
          Create a temp dir
static void deleteDirectory(File directory)
          Recursively delete a directory.
static void deleteDirectory(String directory)
          Recursively delete a directory.
static void forceDelete(File file)
          Delete a file.
static void forceDelete(String file)
          Delete a file.
static void forceDeleteOnExit(File file)
          Schedule a file to be deleted when JVM exits.
static void forceMkdir(File file)
          Make a directory.
static String getExtension(String filename)
          Get extension from filename.
static String getPath(String filepath)
          Get path from filename.
static String getPath(String filepath, char fileSeparatorChar)
          Get path from filename.
static String normalize(String path)
          Normalize a path.
static String readIntoString(File file)
          Read a whole stream into a string
static String readIntoString(Reader reader)
          Read a whole stream into a string
static FileUtil.TextfileIterator readlines(File file)
          Easily iterate over the lines in a textfile using a FileReader Removing is not supported, call remove on the iterable acts as a close.
static String removeExtension(String filename)
          Remove extension from filename.
static String removePath(String filepath)
          Remove path from filename.
static String removePath(String filepath, char fileSeparatorChar)
          Remove path from filename.
static File resolveFile(File baseFile, String filename)
          Resolve a file filename to it's canonical form.
static String sanatizeFilename(String filename)
           
static long sizeOfDirectory(File directory)
          Recursively count size of a directory.
static long sizeOfDirectory(String directory)
          Recursively count size of a directory.
static String stripExtention(String filename)
           
static File toFile(URL url)
          Convert from a URL to a File.
static URL[] toURLs(File[] files)
          Convert the array of Files into a list of URLs.
static void writeToFile(File file, boolean append, String text)
          Write text to file
static void writeToFile(String filename, boolean append, String text)
          Write text to file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readIntoString

public static String readIntoString(File file)
                             throws IOException,
                                    FileNotFoundException
Read a whole stream into a string

Parameters:
file -
Returns:
Throws:
IOException
FileNotFoundException

readIntoString

public static String readIntoString(Reader reader)
                             throws IOException
Read a whole stream into a string

Parameters:
reader -
Returns:
Throws:
IOException

contentEquals

public static boolean contentEquals(File file1,
                                    File file2)
                             throws IOException
Compare the contents of two files to determine if they are equal or not.

Parameters:
file1 - the first file
file2 - the second file
Returns:
true if the content of the files are equal or they both don't exist, false otherwise
Throws:
IOException

toFile

public static File toFile(URL url)
Convert from a URL to a File.

Parameters:
url - File URL.
Returns:
The equivalent File object, or null if the URL's protocol is not file

toURLs

public static URL[] toURLs(File[] files)
                    throws IOException
Convert the array of Files into a list of URLs.

Parameters:
files - the array of files
Returns:
the array of URLs
Throws:
IOException - if an error occurs

removeExtension

public static String removeExtension(String filename)
Remove extension from filename. ie
 foo.txt    --> foo
 a\b\c.jpg --> a\b\c
 a\b\c     --> a\b\c
 

Parameters:
filename - the filename
Returns:
the filename minus extension

getExtension

public static String getExtension(String filename)
Get extension from filename. ie
 foo.txt    --> "txt"
 a\b\c.jpg --> "jpg"
 a\b\c     --> ""
 

Parameters:
filename - the filename
Returns:
the extension of filename or "" if none

removePath

public static String removePath(String filepath)
Remove path from filename. Equivalent to the unix command basename ie.
 a/b/c.txt --> c.txt
 a.txt     --> a.txt
 

Parameters:
filepath - the filepath
Returns:
the filename minus path

removePath

public static String removePath(String filepath,
                                char fileSeparatorChar)
Remove path from filename. ie.
 a/b/c.txt --> c.txt
 a.txt     --> a.txt
 

Parameters:
filepath - the filepath
Returns:
the filename minus path

getPath

public static String getPath(String filepath)
Get path from filename. Roughly equivalent to the unix command dirname. ie.
 a/b/c.txt --> a/b
 a.txt     --> ""
 

Parameters:
filepath - the filepath
Returns:
the filename minus path

getPath

public static String getPath(String filepath,
                             char fileSeparatorChar)
Get path from filename. ie.
 a/b/c.txt --> a/b
 a.txt     --> ""
 

Parameters:
filepath - the filepath
Returns:
the filename minus path

copyFileToDirectory

public static void copyFileToDirectory(String source,
                                       String destinationDirectory)
                                throws IOException
Copy file from source to destination. If destinationDirectory does not exist, it (and any parent directories) will be created. If a file source in destinationDirectory exists, it will be overwritten.

Parameters:
source - An existing File to copy.
destinationDirectory - A directory to copy source into.
Throws:
FileNotFoundException - if source isn't a normal file.
IllegalArgumentException - if destinationDirectory isn't a directory.
IOException - if source does not exist, the file in destinationDirectory cannot be written to, or an IO error occurs during copying.

copyFileToDirectory

public static void copyFileToDirectory(File source,
                                       File destinationDirectory)
                                throws IOException
Copy file from source to destination. If destinationDirectory does not exist, it (and any parent directories) will be created. If a file source in destinationDirectory exists, it will be overwritten.

Parameters:
source - An existing File to copy.
destinationDirectory - A directory to copy source into.
Throws:
FileNotFoundException - if source isn't a normal file.
IllegalArgumentException - if destinationDirectory isn't a directory.
IOException - if source does not exist, the file in destinationDirectory cannot be written to, or an IO error occurs during copying.

copyFile

public static void copyFile(File source,
                            File destination)
                     throws IOException
conveniance

Parameters:
source -
destination -
Throws:
IOException

appendFile

public static void appendFile(File source,
                              File destination)
                       throws IOException
conveniance

Parameters:
source -
destination -
Throws:
IOException

copyFile

public static void copyFile(File source,
                            File destination,
                            boolean append)
                     throws IOException
Copy file from source to destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.

Parameters:
source - An existing non-directory File to copy bytes from.
destination - A non-directory File to write bytes to (possibly overwriting).
Throws:
IOException - if source does not exist, destination cannot be written to, or an IO error occurs during copying.
FileNotFoundException - if destination is a directory (use copyFileToDirectory(java.lang.String, java.lang.String)).

copyURLToFile

public static void copyURLToFile(URL source,
                                 File destination)
                          throws IOException
Copies bytes from the URL source to a file destination. The directories up to destination will be created if they don't already exist. destination will be overwritten if it already exists.

Parameters:
source - A URL to copy bytes from.
destination - A non-directory File to write bytes to (possibly overwriting).
Throws:
IOException - if
  • source URL cannot be opened
  • destination cannot be written to
  • an IO error occurs during copying

normalize

public static final String normalize(String path)
Normalize a path. That means:

Note: note that this method has been tested with unix and windows only.

Eg:

 /foo//               -->     /foo/
 /foo/./              -->     /foo/
 /foo/../bar          -->     /bar
 /foo/../bar/         -->     /bar/
 /foo/../bar/../baz   -->     /baz
 //foo//./bar         -->     /foo/bar
 /../                 -->     null
 

Parameters:
path - the path to be normalized.
Returns:
the normalized path or null.
Throws:
NullPointerException - if path is null.

catPath

public static String catPath(String lookupPath,
                             String path)
Will concatenate 2 paths. Paths with .. will be properly handled.

Eg.,
/a/b/c + d = /a/b/d
/a/b/c + ../d = /a/d

Note: this method handles java/unix style path only (separator is '/').

Returns:
The concatenated paths, or null if error occurs
Throws:
NullPointerException - if any parameter is null.
See Also:
normalize(java.lang.String)

resolveFile

public static File resolveFile(File baseFile,
                               String filename)
Resolve a file filename to it's canonical form. If filename is relative (doesn't start with /), it will be resolved relative to baseFile, otherwise it is treated as a normal root-relative path.

Parameters:
baseFile - Where to resolve filename from, if filename is relative.
filename - Absolute or relative file path to resolve.
Returns:
The canonical File of filename.

forceDelete

public static void forceDelete(String file)
                        throws IOException
Delete a file. If file is directory delete it and all sub-directories.

Throws:
IOException

forceDelete

public static void forceDelete(File file)
                        throws IOException
Delete a file. If file is directory delete it and all sub-directories.

Throws:
IOException

forceDeleteOnExit

public static void forceDeleteOnExit(File file)
                              throws IOException
Schedule a file to be deleted when JVM exits. If file is directory delete it and all sub-directories.

Throws:
IOException

forceMkdir

public static void forceMkdir(File file)
                       throws IOException
Make a directory. If there already exists a file with specified name or the directory is unable to be created then an exception is thrown.

Throws:
IOException

deleteDirectory

public static void deleteDirectory(String directory)
                            throws IOException
Recursively delete a directory.

Throws:
IOException

deleteDirectory

public static void deleteDirectory(File directory)
                            throws IOException
Recursively delete a directory.

Throws:
IOException

cleanDirectory

public static void cleanDirectory(String directory)
                           throws IOException
Clean a directory without deleting it.

Throws:
IOException

cleanDirectory

public static void cleanDirectory(File directory)
                           throws IOException
Clean a directory without deleting it.

Throws:
IOException

sizeOfDirectory

public static long sizeOfDirectory(String directory)
Recursively count size of a directory.

Returns:
size of directory in bytes.

sizeOfDirectory

public static long sizeOfDirectory(File directory)
Recursively count size of a directory.

Returns:
size of directory in bytes.

appendToFile

public static void appendToFile(String filename,
                                String text)
Append text to file

Parameters:
filename -
text -

appendToFile

public static void appendToFile(File file,
                                String text)
Append text to file

Parameters:
file -
text -

writeToFile

public static void writeToFile(String filename,
                               boolean append,
                               String text)
Write text to file

Parameters:
file -
text -
append -

writeToFile

public static void writeToFile(File file,
                               boolean append,
                               String text)
Write text to file

Parameters:
file -
text -
append -

createTempDir

public static File createTempDir(String prefix,
                                 String suffix)
                          throws IOException
Create a temp dir

Parameters:
prefix -
suffix -
Returns:
File of directory
Throws:
IOException

createTempDir

public static File createTempDir()
                          throws IOException
Create temp dir at the default location

Returns:
File of directory
Throws:
IOException

sanatizeFilename

public static String sanatizeFilename(String filename)
Parameters:
filename -
Returns:

stripExtention

public static String stripExtention(String filename)
Parameters:
filename -
Returns:

readlines

public static FileUtil.TextfileIterator readlines(File file)
                                           throws IOException
Easily iterate over the lines in a textfile using a FileReader Removing is not supported, call remove on the iterable acts as a close.

Parameters:
filename -
Returns:
Throws:
IOException


Copyright © 2012 KnowledgePlaza. All Rights Reserved.