Qizx/open API

net.axyana.qizxopen.util
Class FileUtil

java.lang.Object
  extended bynet.axyana.qizxopen.util.FileUtil

public final class FileUtil
extends java.lang.Object

A collection of utility functions (static methods) operating on files and directories.


Constructor Summary
FileUtil()
           
 
Method Summary
static java.lang.String contentTypeToCharsetName(java.lang.String contentType)
          Parses a content type such as "text/html; charset=ISO-8859-1" and returns the name of the IANA charset (that is, the name of the encoding).
static void copyFile(java.io.File srcFile, java.io.File dstFile)
          Copy a file.
static void copyFile(java.lang.String srcFileName, java.lang.String dstFileName)
          Copy a file.
static java.lang.String defaultEncoding()
          Returns the default character encoding for this platform.
static void emptyDirectory(java.io.File dir)
          Recursively deletes all the entries of a directory.
static void emptyDirectory(java.lang.String dirName)
          Recursively deletes all the entries of a directory.
static java.io.File[] expandPathPattern(java.io.File file)
          Expands a simple path pattern containing a '*' or '?' in the FILENAME ONLY.
static java.lang.String fileBaseName(java.lang.String fileName)
          Returns the base name part in a file path name.
static java.lang.String fileDirName(java.lang.String fileName)
          Returns the directory part in a file path name.
static java.lang.String fileExtension(java.lang.String fileName)
          Returns the file extension part in a file path name.
static java.net.URL fileToURL(java.io.File file)
          Converts a File to a file: URL.
static java.net.URL fileToURL(java.lang.String fileName)
          Converts a file name to a file: URL.
static java.lang.String fileToURLName(java.io.File file)
          Converts a File to a file: URL name.
static java.lang.String fileToURLName(java.lang.String fileName)
          Converts a file name to a file: URL name.
static java.lang.String hello(java.lang.String message)
           
static boolean isGzipped(java.io.File file)
          Tests if a file has been compressed using gzip.
static boolean isGzipped(java.lang.String fileName)
          Tests if a file has been compressed using gzip.
static byte[] loadBytes(java.io.File file)
          Loads the content of a binary file.
static byte[] loadBytes(java.lang.String fileName)
          Loads the content of a binary file.
static byte[] loadBytes(java.net.URL url)
          Loads the content of an URL containing binary data.
static byte[] loadBytes(java.net.URL url, boolean interactive)
          Same as loadBytes(java.net.URL), but the interactive argument specifies whether the connection is interactive or not.
static java.lang.String loadGzippedString(java.io.File file)
          Loads the content of a text file compressed using gzip.
static java.lang.String loadGzippedString(java.lang.String fileName)
          Loads the content of a text file compressed using gzip.
static java.lang.String loadGzippedString(java.net.URL url)
          Loads the content of an URL containing text compressed using gzip.
static java.lang.String loadGzippedString(java.net.URL url, boolean interactive)
          Same as loadGzippedString(java.net.URL), but the interactive argument specifies whether the connection is interactive or not.
static java.lang.String loadString(java.io.File file)
          Loads the content of a text file.
static java.lang.String loadString(java.io.InputStream stream, java.lang.String charsetName)
          Loads the content of an InputStream returning text.
static java.lang.String loadString(java.io.Reader in)
           
static java.lang.String loadString(java.lang.String fileName)
          Loads the content of a text file.
static java.lang.String loadString(java.net.URL url)
          Loads the content of an URL containing text.
static java.lang.String loadString(java.net.URL url, boolean interactive)
          Same as loadString(java.net.URL), but the interactive argument specifies whether the connection is interactive or not.
static boolean removeFile(java.io.File file, boolean force)
          Deletes a file or a directory, possibly emptying the directory before deleting it.
static boolean removeFile(java.lang.String fileName)
          Deletes a file or an empty directory.
static boolean removeFile(java.lang.String fileName, boolean force)
          Deletes a file or a directory, possibly emptying the directory before deleting it.
static void saveString(java.lang.String string, java.io.File file)
          Saves some text to a file.
static void saveString(java.lang.String string, java.io.File file, java.lang.String charsetName)
          Saves some text to a file.
static void saveString(java.lang.String string, java.lang.String fileName)
          Saves some text to a file.
static java.net.URL toURL(java.lang.String uri)
          Converts a String to an URL or returns null.
static java.lang.String trimFileExtension(java.lang.String fileName)
          Returns a file path name without its file extension part.
static java.net.URL uriToURL(java.lang.String uri)
          Converts an unknown URI to an URL.
static java.io.File urlToFile(java.lang.String urlName)
          Converts a file: URL name to a File.
static java.io.File urlToFile(java.net.URL url)
          Converts a file: URL to a File.
static java.lang.String urlToFileName(java.lang.String urlName)
          Converts a file: URL name to a file name.
static java.lang.String urlToFileName(java.net.URL url)
          Converts a file: URL to a file name.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

public FileUtil()
Method Detail

urlToFile

public static java.io.File urlToFile(java.net.URL url)
Converts a file: URL to a File.

Parameters:
url - the URL to be converted
Returns:
the result of the conversion or null if url is not a file: URL

urlToFile

public static java.io.File urlToFile(java.lang.String urlName)
Converts a file: URL name to a File.

Parameters:
urlName - the URL name to be converted
Returns:
the result of the conversion or null if urlName is not a file: URL

urlToFileName

public static java.lang.String urlToFileName(java.net.URL url)
Converts a file: URL to a file name.

Parameters:
url - the URL to be converted
Returns:
the result of the conversion or null if url is not a file: URL

urlToFileName

public static java.lang.String urlToFileName(java.lang.String urlName)
Converts a file: URL name to a file name.

Parameters:
urlName - the URL name to be converted
Returns:
the result of the conversion or null if urlName is not a file: URL

fileToURL

public static java.net.URL fileToURL(java.io.File file)
Converts a File to a file: URL.

Parameters:
file - the file to be converted
Returns:
the result of the conversion

fileToURL

public static java.net.URL fileToURL(java.lang.String fileName)
Converts a file name to a file: URL.

Parameters:
fileName - the file name to be converted
Returns:
the result of the conversion

uriToURL

public static java.net.URL uriToURL(java.lang.String uri)
Converts an unknown URI to an URL.

Parameters:
uri - to be converted
Returns:
the result of the conversion

toURL

public static java.net.URL toURL(java.lang.String uri)
Converts a String to an URL or returns null.

Parameters:
uri - to be converted
Returns:
the result of the conversion

fileToURLName

public static java.lang.String fileToURLName(java.io.File file)
Converts a File to a file: URL name.

Parameters:
file - the file to be converted
Returns:
the result of the conversion

fileToURLName

public static java.lang.String fileToURLName(java.lang.String fileName)
Converts a file name to a file: URL name.

Parameters:
fileName - the file name to be converted
Returns:
the result of the conversion

fileDirName

public static java.lang.String fileDirName(java.lang.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)

Parameters:
fileName - a file path name
Returns:
the directory part

fileBaseName

public static java.lang.String fileBaseName(java.lang.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 in addition to '\'.

Path Result
util/FileUtil.java FileUtil.java
FileUtil.java FileUtil.java

Parameters:
fileName - a file path name
Returns:
the base name part

fileExtension

public static java.lang.String fileExtension(java.lang.String fileName)
Returns the file extension part in a file path name. The file extension part is everything after the last '.'.

On Windows, '/' is used as an alternate file path name separator in addition to '\'.

Path Result
util/FileUtil.java .java
makefile "" (empty string)
/home/hussein/.profile "" (empty string)

Parameters:
fileName - a file path name
Returns:
the file extension part; does not include the '.'

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.


trimFileExtension

public static java.lang.String trimFileExtension(java.lang.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

Parameters:
fileName - a file path name
Returns:
the file path without extension part if any

expandPathPattern

public static java.io.File[] expandPathPattern(java.io.File file)
Expands a simple path pattern containing a '*' or '?' in the FILENAME ONLY.


removeFile

public static boolean removeFile(java.lang.String fileName)
Deletes a file or an empty directory.

Parameters:
fileName - the name of the file or empty directory to be deleted
Returns:
true if the file or directory has been successfully deleted; false otherwise

removeFile

public static boolean removeFile(java.lang.String fileName,
                                 boolean force)
Deletes a file or a directory, possibly emptying the directory before deleting it.

Parameters:
fileName - the name of the file or directory to be deleted
force - 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
Returns:
true if the file or directory has been successfully deleted; false otherwise

removeFile

public static boolean removeFile(java.io.File file,
                                 boolean force)
Deletes a file or a directory, possibly emptying the directory before deleting it.

Parameters:
file - the file or directory to be deleted
force - 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
Returns:
true if the file or directory has been successfully deleted; false otherwise

emptyDirectory

public static void emptyDirectory(java.lang.String dirName)
Recursively deletes all the entries of a directory.

Parameters:
dirName - the name of the directory to be emptied

emptyDirectory

public static void emptyDirectory(java.io.File dir)
Recursively deletes all the entries of a directory.

Parameters:
dir - the directory to be emptied

copyFile

public static void copyFile(java.lang.String srcFileName,
                            java.lang.String dstFileName)
                     throws java.io.IOException
Copy a file.

Parameters:
srcFileName - the name of the source file
dstFileName - the name of the destination file
Throws:
java.io.IOException - if there is an IO problem

copyFile

public static void copyFile(java.io.File srcFile,
                            java.io.File dstFile)
                     throws java.io.IOException
Copy a file.

Parameters:
srcFile - source file
dstFile - destination file
Throws:
java.io.IOException - if there is an IO problem

loadString

public static java.lang.String loadString(java.lang.String fileName)
                                   throws java.io.IOException
Loads the content of a text file. The encoding of the text is assumed to be the native encoding of the platform.

Parameters:
fileName - the name of the text file
Returns:
the loaded String
Throws:
java.io.IOException - if there is an IO problem

loadString

public static java.lang.String loadString(java.io.File file)
                                   throws java.io.IOException
Loads the content of a text file. The encoding of the text is assumed to be the native encoding of the platform.

Parameters:
file - the text file
Returns:
the loaded String
Throws:
java.io.IOException - if there is an IO problem

loadString

public static java.lang.String loadString(java.net.URL url)
                                   throws java.io.IOException
Loads the content of an URL containing text.

Parameters:
url - the URL of the text resource
Returns:
the loaded String
Throws:
java.io.IOException - if there is an IO problem

loadString

public static java.lang.String loadString(java.net.URL url,
                                          boolean interactive)
                                   throws java.io.IOException
Same as loadString(java.net.URL), but the interactive argument specifies whether the connection is interactive or not. For example, an interactive HTTP connection may display a dialog box to let the user specify his user name and his password.

Note that in loadString(java.net.URL), the connection is not interactive.

Throws:
java.io.IOException

contentTypeToCharsetName

public static java.lang.String contentTypeToCharsetName(java.lang.String contentType)
Parses a content type such as "text/html; charset=ISO-8859-1" and returns the name of the IANA charset (that is, the name of the encoding).

Parameters:
contentType - the content type to be parsed
Returns:
the name of the IANA charset if parsing was successful or null otherwise.

loadString

public static java.lang.String loadString(java.io.InputStream stream,
                                          java.lang.String charsetName)
                                   throws java.io.IOException
Loads the content of an InputStream returning text.

Parameters:
stream - the text source
charsetName - the IANA charset of the text source if known; null may be used to specify the native encoding of the platform
Returns:
the loaded String
Throws:
java.io.IOException - if there is an IO problem

loadString

public static java.lang.String loadString(java.io.Reader in)
                                   throws java.io.IOException
Throws:
java.io.IOException

saveString

public static void saveString(java.lang.String string,
                              java.lang.String fileName)
                       throws java.io.IOException
Saves some text to a file.

Parameters:
string - the text to be saved
fileName - the name of the destination file
Throws:
java.io.IOException - if there is an IO problem

saveString

public static void saveString(java.lang.String string,
                              java.io.File file)
                       throws java.io.IOException
Saves some text to a file.

Parameters:
string - the text to be saved
file - the destination file
Throws:
java.io.IOException - if there is an IO problem

saveString

public static void saveString(java.lang.String string,
                              java.io.File file,
                              java.lang.String charsetName)
                       throws java.io.IOException
Saves some text to a file.

Parameters:
string - the text to be saved
file - the destination file
charsetName - the IANA charset of the saved file; null may be used to specify the native encoding of the platform
Throws:
java.io.IOException - if there is an IO problem

loadBytes

public static byte[] loadBytes(java.lang.String fileName)
                        throws java.io.IOException
Loads the content of a binary file.

Parameters:
fileName - the name of the binary file
Returns:
the loaded bytes
Throws:
java.io.IOException - if there is an IO problem

loadBytes

public static byte[] loadBytes(java.io.File file)
                        throws java.io.IOException
Loads the content of a binary file.

Parameters:
file - the binary file
Returns:
the loaded bytes
Throws:
java.io.IOException - if there is an IO problem

loadBytes

public static byte[] loadBytes(java.net.URL url)
                        throws java.io.IOException
Loads the content of an URL containing binary data.

Parameters:
url - the URL of the binary data
Returns:
the loaded bytes
Throws:
java.io.IOException - if there is an IO problem

loadBytes

public static byte[] loadBytes(java.net.URL url,
                               boolean interactive)
                        throws java.io.IOException
Same as loadBytes(java.net.URL), but the interactive argument specifies whether the connection is interactive or not. For example, an interactive HTTP connection may display a dialog box to let the user specify his user name and his password.

Note that in loadBytes(java.net.URL), the connection is not interactive.

Throws:
java.io.IOException

isGzipped

public static boolean isGzipped(java.lang.String fileName)
                         throws java.io.IOException
Tests if a file has been compressed using gzip.

Parameters:
fileName - the name of the file to be tested
Returns:
true if the file has been gzip-ed, false otherwise
Throws:
java.io.IOException - if there is an IO problem

isGzipped

public static boolean isGzipped(java.io.File file)
                         throws java.io.IOException
Tests if a file has been compressed using gzip.

Parameters:
file - the file to be tested
Returns:
true if the file has been gzip-ed, false otherwise
Throws:
java.io.IOException - if there is an IO problem

loadGzippedString

public static java.lang.String loadGzippedString(java.lang.String fileName)
                                          throws java.io.IOException
Loads the content of a text file compressed using gzip.

Parameters:
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
Returns:
the loaded String
Throws:
java.io.IOException - if there is an IO problem
See Also:
defaultEncoding()

loadGzippedString

public static java.lang.String loadGzippedString(java.io.File file)
                                          throws java.io.IOException
Loads the content of a text file compressed using gzip.

Parameters:
file - the gzip-ed file; the encoding of the text before compression is assumed to be the default encoding of the platform
Returns:
the loaded String
Throws:
java.io.IOException - if there is an IO problem
See Also:
defaultEncoding()

loadGzippedString

public static java.lang.String loadGzippedString(java.net.URL url)
                                          throws java.io.IOException
Loads the content of an URL containing text compressed using gzip.

Parameters:
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
Returns:
the loaded String
Throws:
java.io.IOException - if there is an IO problem
See Also:
defaultEncoding()

loadGzippedString

public static java.lang.String loadGzippedString(java.net.URL url,
                                                 boolean interactive)
                                          throws java.io.IOException
Same as loadGzippedString(java.net.URL), but the interactive argument specifies whether the connection is interactive or not. For example, an interactive HTTP connection may display a dialog box to let the user specify his user name and his password.

Note that in loadGzippedString(java.net.URL), the connection is not interactive.

Throws:
java.io.IOException

defaultEncoding

public static java.lang.String defaultEncoding()
Returns the default character encoding for this platform.

Returns:
default character encoding for this platform

hello

public static java.lang.String hello(java.lang.String message)

© 2005 Axyana Software