Package com.nomagic.magicreport.helper
Class FileUtils
java.lang.Object
com.nomagic.magicreport.helper.FileUtils
A collection of utility methods for File handler.
- Since:
- 1.0 Nov 20, 2006
- Version:
- May 17, 2007
-
Method Summary
Modifier and TypeMethodDescriptionstatic intcopy(InputStream input, OutputStream output) Copy bytes from anInputStreamto anOutputStream.static intCopy chars from aReaderto aWriter.static voidcreateDirectory(File file) Create a directory denoted by abstract pathname.static FilecreateMrTempFile(String prefix, String suffix) Create temporary file in thegetMrTempDir()directory.static FilecreateNonOverwriteFile(File parent, String child) Create a non-overwrite file from given name and extension.static FilecreateNonOverwriteFile(File parent, String prefix, String suffix) Create a non-overwrite file from given name and extension.static FilecreateTempFile(String prefix, String suffix, String parent, boolean useUnique)
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.static booleanDeletes the file or directory denoted by this abstract pathname.static StringgetExtension(File file) Return extension of file.static StringgetExtension(String filename) Return extension of file.static StringGet MagicReport temporary directory.static StringReturn name of file without extension.static StringReturn system temporary directory.The default temporary-file directory is specified by the system propertyjava.io.tmpdir.static FilegetTemplateFile(String templateFileName, String templateLocation) Get Template file from template file name/path.static FilegetTemplateFile(String templateFileName, String templateLocation, String extension, Object marker) Get Template file from template file name/path.static FilegetTemplateFile(String templateFileName, List<String> templateLocations, String extension, Object marker) Get Template file from template file name/path.static FileConvert the given file to normal form.static StringConvert the given name string to normal form.static File[]recursiveFileList(File basedDir) Recursive to get all files inbasedDir.static URL[]recursiveFileURLList(File basedDir) Recursive to get all resources inbasedDir.static voidTrack the specified file, using the provided marker, deleting the file when the marker instance is garbage collected.static voidTrack the specified file, using the provided marker, deleting the file when the marker instance is garbage collected.
-
Method Details
-
createNonOverwriteFile
Create a non-overwrite file from given name and extension. The format of new file name is name + "." + extension. If file already exists, append (number) after name.
Example: createNonOverwriteFile(parent, "filename", "txt"); // result of this method is "filename.txt" // if "filename.txt" already exists, the new file name is "filename(1).txt"- Parameters:
parent- The directory in which the file is to be createdprefix- The prefix string to be used in generating the file's namesuffix- The suffix string to be used in generating the file's name; may be extension of file.- Returns:
- an instance of File
-
createNonOverwriteFile
Create a non-overwrite file from given name and extension. The format of new file name is name + "." + extension. If file already exists, append (number) after name.
Example: createNonOverwriteFile(parent, "filename", "txt"); // result of this method is "filename.txt" // if "filename.txt" already exists, the new file name is "filename(1).txt"- Parameters:
parent- The directory in which the file is to be createdchild- The child pathname string- Returns:
- an instance of File
-
getExtension
Return extension of file. Extension excluded "." character.- Parameters:
file- file to find an extension- Returns:
- extension of file
-
getExtension
Return extension of file. Extension excluded "." character.- Parameters:
filename- file name- Returns:
- extension of file
-
getName
Return name of file without extension.- Parameters:
filename- file name- Returns:
- name of file
-
recursiveFileURLList
Recursive to get all resources inbasedDir. Return array of fileURL, or array length zero if no file found.- Parameters:
basedDir- root directory- Returns:
arrayof fileURL
-
recursiveFileList
Recursive to get all files inbasedDir. Return array ofFile, or array length zero if no file found.- Parameters:
basedDir- root directory- Returns:
arrayofFile
-
createTempFile
public static File createTempFile(String prefix, String suffix, String parent, boolean useUnique) throws IOException
Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name. If this method returns successfully then it is guaranteed that:- The file denoted by the returned abstract pathname did not exist before this method was invoked, and
- Neither this method nor any of its variants will return the same abstract pathname again in the current invocation of the virtual machine.
ortrack(File,Object)java.io.File#deleteOnExit()method.
If theparentargument isnullthen the system-dependent default temporary-file directory will be used. The default temporary-file directory is specified by the system propertyjava.io.tmpdir.
The complete structure of empty file is:prefix + unique_number + suffix- Parameters:
prefix- The prefix string to be used in generating the file's name; must be at least three characters longsuffix- The suffix string to be used in generating the file's name; may benull, in which case the suffix".tmp"will be usedparent- The directory in which the file is to be created, ornullif the default temporary-file directory is to be useduseUnique- if true always create unique file name.- Returns:
- An abstract pathname denoting a newly-created empty file
- Throws:
IllegalArgumentException- If theprefixargument contains fewer than three charactersIOException- If a file could not be created
-
createMrTempFile
Create temporary file in thegetMrTempDir()directory.This equals to
createTempFile(prefix, suffix, getMrTempDir(), true).- Parameters:
prefix- The prefix string to be used in generating the file's name; must be at least three characters longsuffix- The suffix string to be used in generating the file's name; may benull, in which case the suffix".tmp"will be used- Returns:
- An abstract pathname denoting a newly-created empty file
- Throws:
IOException- If a file could not be created
-
getTempDir
Return system temporary directory.The default temporary-file directory is specified by the system propertyjava.io.tmpdir. On UNIX systems the default value of this property is typically "/tmp" or "/var/tmp"; on Microsoft Windows systems it is typically "C:\\WINNT\\TEMP". A different value may be given to this system property when the Java virtual machine is invoked- Returns:
- default temporary-file directory
-
getMrTempDir
Get MagicReport temporary directory.The MagicReport directory is
. The directory will be automatically created if it doesn't exist.getTempDir()+"mr/"- Returns:
- MagicReport temporary directory.
- See Also:
-
copy
Copy bytes from anInputStreamto anOutputStream.
This method buffers the input internally, so there is no need to use aBufferedInputStream.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write to- Returns:
- the number of bytes copied
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs
-
copy
Copy chars from aReaderto aWriter.
This method buffers the input internally, so there is no need to use aBufferedReader.- Parameters:
input- theReaderto read fromoutput- theWriterto write to- Returns:
- the number of characters copied
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs
-
delete
Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the file and directory inside this file will be deleted recursively.- Parameters:
file- the file or directory to be deleted- Returns:
trueif and only if the file or directory is successfully deleted;falseotherwise- Throws:
SecurityException- If a security manager exists and itsmethod denies delete access to the fileSecurityManager.checkDelete(java.lang.String)
-
createDirectory
Create a directory denoted by abstract pathname. This method automatically remove special characters in name before create a directory. Special characters are one of the following:\/?*:"<>|- Parameters:
file- directory to be created.- Throws:
IOException- if false to create directory.
-
normalize
Convert the given file to normal form. If the string is already in normal form then it is simply returned.- Parameters:
file- file- Returns:
- normalized name
-
normalize
Convert the given name string to normal form. If the string is already in normal form then it is simply returned.- Parameters:
name- name of file- Returns:
- normalized name
-
track
Track the specified file, using the provided marker, deleting the file when the marker instance is garbage collected.- Parameters:
file- The file to be tracked.marker- The marker object used to track the file.
-
track
Track the specified file, using the provided marker, deleting the file when the marker instance is garbage collected.- Parameters:
path- The full path to the file to be tracked.marker- The marker object used to track the file.
-
getTemplateFile
Get Template file from template file name/path.- Parameters:
templateFileName- template file name or pathtemplateLocation- template location- Returns:
- template file
-
getTemplateFile
public static File getTemplateFile(String templateFileName, String templateLocation, String extension, Object marker) Get Template file from template file name/path.- Parameters:
templateFileName- template file name or pathtemplateLocation- template locationextension- extension for template file. in case it has no extensionmarker- The marker object used to track the file.- Returns:
- template file
-
getTemplateFile
public static File getTemplateFile(String templateFileName, List<String> templateLocations, String extension, Object marker) Get Template file from template file name/path.- Parameters:
templateFileName- template file name or pathtemplateLocations- list of possible locationsextension- extension for template file. in case it has no extensionmarker- The marker object used to track the file.- Returns:
- template file
-