A collection of utility functions (static methods) operating on files and
directories.
copyFile
public static void copyFile(String srcFileName,
String dstFileName)
throws IOException
Copy a file.
srcFileName
- the name of the copied filedstFileName
- the name of the copy
defaultEncoding
public static String defaultEncoding()
Returns the default character encoding for this platform.
- default character encoding for this platform
emptyDirectory
public static void emptyDirectory(File dir)
Recursively deletes all the entries of a directory.
dir
- the directory to be emptied
emptyDirectory
public static void emptyDirectory(String dirName)
Recursively deletes all the entries of a directory.
dirName
- the name of the directory to be emptied
fileBaseName
public static String fileBaseName(String fileName)
Returns the base name part in a file path name. The base name part is
everything after the last file name separator (that is
'\' for
Windows).
On Windows,
'/' is used as an alternate file path name
separator.
Path
| Result
|
---|
util/FileUtil.java
| FileUtil.java
|
FileUtil.java
| FileUtil.java
|
fileName
- a file path name
fileDirName
public static String fileDirName(String fileName)
Returns the directory part in a file path name. The directory part is
everything before the last file path name separator (that is,
'\' for Windows).
On Windows,
'/' is used as an alternate file path name
separator.
Examples:
Path
| Result
|
---|
util/FileUtil.java
| util
|
FileUtil.java
| . (dot)
|
fileName
- a file path name
fileExtension
public static String fileExtension(String fileName)
Returns the file extension part in a file path name. The file extension
part is everything after the last dot.
On Windows,
'/' is used as an alternate file path name
separator.
Path
| Result
|
---|
util/FileUtil.java
| .java
|
makefile
| "" (empty string)
|
/home/hussein/.profile
| "" (empty string)
|
fileName
- a file path name
- the file extension part; does not include the dot
If the base name without its extension is empty, the path is
considered not to have an extension part. This is the case of
/home/hussein/.profile in the examples above.
fileToURL
public static URL fileToURL(File file)
Converts a File to a file: URL.
file
- the file to be converted
- the result of the conversion
fileToURL
public static URL fileToURL(String fileName)
Converts a file name to a file: URL.
fileName
- the file name to be converted
- the result of the conversion
fileToURLName
public static String fileToURLName(String fileName)
Converts a file name to a file: URL name.
fileName
- the file name to be converted
- the result of the conversion
isGzipped
public static boolean isGzipped(String fileName)
throws IOException
Tests if a file has been compressed using gzip.
fileName
- the name of the file to be tested
true
if the file has been gzip-ed,
false
otherwise
loadBytes
public static byte[] loadBytes(InputStream source)
throws IOException
Loads the content of an InputStream returning binary data.
source
- the binary data source
loadBytes
public static byte[] loadBytes(String fileName)
throws IOException
Loads the content of a binary file.
fileName
- the name of the binary file
loadBytes
public static byte[] loadBytes(URL url)
throws IOException
Loads the content of an URL containing binary data.
url
- the URL of the binary data
loadGzippedString
public static String loadGzippedString(InputStream source,
String encoding)
throws IOException
Loads the content of an InputStream returning text compressed using
gzip.
source
- the gzip-ed data sourceencoding
- the encoding of the text before compression
loadGzippedString
public static String loadGzippedString(String fileName)
throws IOException
Loads the content of a text file compressed using gzip.
fileName
- the name of the gzip-ed file; the encoding of the text
before compression is assumed to be the default encoding of the
platform
loadGzippedString
public static String loadGzippedString(URL url)
throws IOException
Loads the content of an URL containing text compressed using gzip.
url
- the URL of the gzip-ed data; the encoding of the text before
compression is assumed to be the default encoding of the platform
loadString
public static String loadString(InputStream stream)
throws IOException
Loads the content of an InputStream returning text.
loadString
public static String loadString(String fileName)
throws FileNotFoundException,
IOException
Loads the content of a text file.
fileName
- the name of the text file
loadString
public static String loadString(URL url)
throws IOException
Loads the content of an URL containing text.
url
- the URL of the text resource
main
public static void main(String[] args)
A simple test for all functions dealing with file path names.
removeFile
public static boolean removeFile(File file,
boolean force)
Deletes a file or a directory, possibly emptying the directory before
deleting it.
file
- the file or directory to be deletedforce
- if true
and the file to be deleted is a
non-empty directory, empty it before attempting to delete it; if
false
, do not empty directories
true
if the file or directory has been
successfully deleted; false
otherwise
removeFile
public static boolean removeFile(String fileName)
Deletes a file or an empty directory.
fileName
- the name of the file or empty directory to be deleted
true
if the file or directory has been
successfully deleted; false
otherwise
removeFile
public static boolean removeFile(String fileName,
boolean force)
Deletes a file or a directory, possibly emptying the directory before
deleting it.
fileName
- the name of the file or directory to be deletedforce
- if true
and the file to be deleted is a
non-empty directory, empty it before attempting to delete it; if
false
, do not empty directories
true
if the file or directory has been
successfully deleted; false
otherwise
saveString
public static void saveString(String string,
OutputStream stream)
throws IOException
Saves some text to an OutputStream.
string
- the text to be savedstream
- the text sink
saveString
public static void saveString(String string,
String fileName)
throws IOException
Saves some text to a file.
string
- the text to be savedfileName
- the name of the file
trimFileExtension
public static String trimFileExtension(String fileName)
Returns a file path name without its file extension part. The file
extension part is everything after the last dot.
On Windows,
'/' is used as an alternate file path name
separator.
Path
| Result
|
---|
util/FileUtil.java
| util/FileUtil
|
makefile
| makefile
|
/home/hussein/.profile
| /home/hussein/.profile
|
fileName
- a file path name
- the file path without extension part if any
urlToFile
public static File urlToFile(String urlName)
Converts a file: URL name to a File.
urlName
- the URL name to be converted
- the result of the conversion or
null
if
urlName
is not a file: URL
urlToFile
public static File urlToFile(URL url)
Converts a file: URL to a File.
url
- the URL to be converted
- the result of the conversion or
null
if
url
is not a file: URL
urlToFileName
public static String urlToFileName(String urlName)
Converts a file: URL name to a file name.
urlName
- the URL name to be converted
- the result of the conversion or
null
if
urlName
is not a file: URL