|
SoftNet-Consult Java Utility Library | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.softnetConsult.utils.files.FileTools
public final class FileTools
This is a collection of static utility methods for dealing with files and parths.
This product includes software developed by the
SoftNet-Consult Java Utility Library project and its contributors.
(http://java-tools.sourceforge.net)
Copyright (c) 2007-2008 SoftNet-Consult.
Copyright (c) 2007-2008 G. Paperin.
All rights reserved.
File: FileTools.java
Library API version: "2.02"
Java compliance version: "1.5"
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following terms and conditions are met:
1. Redistributions of source code must retain the above
acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above acknowledgement of the
SoftNet-Consult Java Utility Library project, the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
3. All advertising materials mentioning features or use of this software or any derived
software must display the following acknowledgement:
This product includes software developed by the SoftNet-Consult Java Utility Library
project and its contributors.
THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Field Summary | |
---|---|
static java.lang.String |
FILE_EXTENSION_SEPARATOR
|
static java.lang.String |
FILE_NAME_UNIQUE_MAKER_TAG
|
Constructor Summary | |
---|---|
private |
FileTools()
Prevents instances of this class from being created as this class contains only static utility methods. |
Method Summary | |
---|---|
static java.lang.String |
concatDirFile(java.lang.String dirName,
java.lang.String fileName)
Concatenates a directory name and a file name to build a path by inserting a file separator ( '/' or '\\' ) between them if necessary.For example all of the following calls will produce the same result: - concatDirFile("dir/subdir", "file.name") - concatDirFile("dir/subdir/", "file.name") - concatDirFile("dir/subdir", "/file.name") - concatDirFile("dir/subdir/", "/file.name") RESULT: "dir/subdir/file.name" Note that '/' is used in the example, but '\\' is equally supported. |
static java.lang.String |
concatFileExt(java.lang.String fileBaseName,
java.lang.String fileExt)
Concatenates a file base nameand a file extension to build a full file name by inserting a file extension separator ( '.' ) between them if necessary.For example all of the following calls will produce the same result: - concatDirFile("basename", "ext") - concatDirFile("basename.", "ext") - concatDirFile("basename", ".ext") - concatDirFile("basename.", ".ext") RESULT: "basename.ext" |
static boolean |
deleteAllRecursively(java.io.File file)
Deletes the specified file; if the file specifies a directory all contained files and subdirectories are also deleted recursively. |
static boolean |
deleteAllRecursively(java.io.File file,
java.util.List<java.io.File> deleted,
java.util.List<java.io.File> failed)
Deletes the specified file; if the file specifies a directory all contained files and subdirectories are also deleted recursively. |
static boolean |
deleteAllRecursively(java.lang.String path)
Deletes the specified file; if the path specifies a directory all contained files and subdirectories are also deleted recursively. |
static boolean |
deleteAllRecursively(java.lang.String path,
java.util.List<java.io.File> deleted,
java.util.List<java.io.File> failed)
Deletes the specified file; if the path specifies a directory all contained files and subdirectories are also deleted recursively. |
static boolean |
deleteRecursively(java.io.File file)
Deletes the specified file, if the file specifies a directory all contained files and subdirectories are also deleted recursively. |
static boolean |
deleteRecursively(java.lang.String path)
Deletes the specified file, if the file specifies a directory all contained files and subdirectories are also deleted recursively. |
static java.io.File |
findUniqueFile(java.lang.String fileName,
java.lang.String fileExt,
boolean alwaysUseTimestamp)
Creates a File object describing a file with the specified base name and extension
as well as an optional timestamp and an optional counter to make the file
name unique; the program start directory is used for the path.This methos works generally in the same way as findUniqueFileName(String, String, String, boolean) and is equivalent to
findUniqueFile(System.getProperty("user.dir"), fileName, fileExt, alwaysUseTimestamp) . |
static java.io.File |
findUniqueFile(java.lang.String fileDir,
java.lang.String fileName,
java.lang.String fileExt,
boolean alwaysUseTimestamp)
Creates a File object describing a file with the specified base name, extension
and directory as well as an optional timestamp and an optional counter to make the file
name unique. |
static java.lang.String |
findUniqueFileName(java.lang.String fileName,
java.lang.String fileExt,
boolean alwaysUseTimestamp)
Creates an absolute file name describing a file with the specified base name and extension as well as an optional timestamp and an optional counter to make the file name unique; the program start directory is used for the path. This methos works generally in the same way as findUniqueFileName(String, String, String, boolean) and is equivalent to
findUniqueFile(fileName, fileExt, alwaysUseTimestamp).getAbsolutePath() . |
static java.lang.String |
findUniqueFileName(java.lang.String fileDir,
java.lang.String fileName,
java.lang.String fileExt,
boolean alwaysUseTimestamp)
Creates an absolute file name describing a file with the specified base name, extension and directory as well as an optional timestamp and an optional counter to make the file name unique. This methos works generally in the same way as findUniqueFileName(String, String, String, boolean) and is equivalent to
findUniqueFile(fileDir, fileName, fileExt, alwaysUseTimestamp).getAbsolutePath() . |
static java.lang.String |
getBaseName(java.io.File file)
Gets the base name of the specified name. |
static java.lang.String |
getBaseName(java.lang.String fileName)
Gets the base name of the specified file name. |
static java.lang.String |
getExtension(java.io.File file)
Gets the extension of the specified file including the extension delimeter ("."). |
static java.lang.String |
getExtension(java.io.File file,
boolean includeExtSep)
Gets the extension of the specified file. |
static java.lang.String |
getExtension(java.lang.String fileName)
Gets the extension of the specified file name including the extension delimeter ("."). |
static java.lang.String |
getExtension(java.lang.String fileName,
boolean includeExtSep)
Gets the extension of the specified file name. |
static boolean |
writeToFile(java.lang.String fileName,
java.lang.String str)
Writes a specified string to the specified file; the file is always overwritten. |
static boolean |
writeToFile(java.lang.String fileName,
java.lang.String str,
boolean append)
Writes a specified string to the specified file. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String FILE_EXTENSION_SEPARATOR
public static final java.lang.String FILE_NAME_UNIQUE_MAKER_TAG
Constructor Detail |
---|
private FileTools()
Method Detail |
---|
public static java.lang.String getExtension(java.lang.String fileName)
fileName
- A file name.
public static java.lang.String getExtension(java.lang.String fileName, boolean includeExtSep)
fileName
- A file name.includeExtSep
- Whether the extension delimeter
(".") will be returned as part of the
extension.
public static java.lang.String getExtension(java.io.File file)
file
- A file
public static java.lang.String getExtension(java.io.File file, boolean includeExtSep)
file
- A file.includeExtSep
- Whether the extension delimeter
(".") will be returned as part of the
extension.
public static java.lang.String getBaseName(java.lang.String fileName)
fileName
- A file name.
public static java.lang.String getBaseName(java.io.File file)
file
- A file.
public static final java.lang.String concatDirFile(java.lang.String dirName, java.lang.String fileName)
'/'
or '\\'
) between them if necessary.concatDirFile("dir/subdir", "file.name")
concatDirFile("dir/subdir/", "file.name")
concatDirFile("dir/subdir", "/file.name")
concatDirFile("dir/subdir/", "/file.name")
"dir/subdir/file.name"
'/'
is used in the example, but '\\'
is equally supported.
dirName
- A directory name.fileName
- A file name
public static final java.lang.String concatFileExt(java.lang.String fileBaseName, java.lang.String fileExt)
'.'
) between them if necessary.concatDirFile("basename", "ext")
concatDirFile("basename.", "ext")
concatDirFile("basename", ".ext")
concatDirFile("basename.", ".ext")
"basename.ext"
fileBaseName
- A file base name.fileExt
- A file extension.
public static java.lang.String findUniqueFileName(java.lang.String fileName, java.lang.String fileExt, boolean alwaysUseTimestamp)
findUniqueFileName(String, String, String, boolean)
and is equivalent to
findUniqueFile(fileName, fileExt, alwaysUseTimestamp).getAbsolutePath()
.
fileName
- The base name for the unique albolute file name to be created.fileExt
- The extension for the unique albolute file name to be created.alwaysUseTimestamp
- Whether to use a timestamp if no timestamp position marker
is specified.
public static java.lang.String findUniqueFileName(java.lang.String fileDir, java.lang.String fileName, java.lang.String fileExt, boolean alwaysUseTimestamp)
findUniqueFileName(String, String, String, boolean)
and is equivalent to
findUniqueFile(fileDir, fileName, fileExt, alwaysUseTimestamp).getAbsolutePath()
.
fileDir
- The directory for the unique albolute file name to be created.fileName
- The base name for the unique albolute file name to be created.fileExt
- The extension for the unique-path File
object to be created.alwaysUseTimestamp
- Whether to use a timestamp if no timestamp position marker
is specified.
public static java.io.File findUniqueFile(java.lang.String fileName, java.lang.String fileExt, boolean alwaysUseTimestamp)
File
object describing a file with the specified base name and extension
as well as an optional timestamp and an optional counter to make the file
name unique; the program start directory is used for the path.findUniqueFileName(String, String, String, boolean)
and is equivalent to
findUniqueFile(System.getProperty("user.dir"), fileName, fileExt, alwaysUseTimestamp)
.
fileName
- The base name for the unique-path File
object to be created.fileExt
- The extension for the unique-path File
object to be created.alwaysUseTimestamp
- Whether to use a timestamp if no timestamp position marker
is specified.
File
object decribing a path based on the specified parameters
modified to make sure that no file with the same name/path already exists.public static java.io.File findUniqueFile(java.lang.String fileDir, java.lang.String fileName, java.lang.String fileExt, boolean alwaysUseTimestamp)
File
object describing a file with the specified base name, extension
and directory as well as an optional timestamp and an optional counter to make the file
name unique. This method works as follows:directory/basename.ext
").alwaysUseTimestamp
is true
, a timestamp
is inserted after the filename and before the extension. If the specified file name does not
contain a timestamp marker and alwaysUseTimestamp
is false
, no timespamp is
inserted. Executed on 20 Jan 2008 at 18:30:15 the result may look like this:
"directory/basename.08.01.20-18.30.15.ext
".File
object is returned. If a file with the resulting path exists, a counter is inserted just
before the extension. The result may look like this:
"directory/basename.08.01.20-18.30.15(1).ext
".
fileDir
- The directory for the unique-path File
object to be created.fileName
- The base name for the unique-path File
object to be created.fileExt
- The extension for the unique-path File
object to be created.alwaysUseTimestamp
- Whether to use a timestamp if no timestamp position marker
is specified.
File
object decribing a path based on the specified parameters
modified as above to make sure that no file with the same name/path already exists.public static boolean writeToFile(java.lang.String fileName, java.lang.String str)
fileName
- The name of the file to write to.str
- The string to write to the file.
false
if the specified file name was null
or if an IO exception
occured; true
otherwise.public static boolean writeToFile(java.lang.String fileName, java.lang.String str, boolean append)
fileName
- The name of the file to write to.str
- The string to write to the file.append
- If true
than the string will be appended at the end of the file,
otherwise the file will be overwritten.
false
if the specified file name was null
or if an IO exception
occured; true
otherwise.public static boolean deleteRecursively(java.lang.String path)
deleteAllRecursively
-methods in this class
that proceed attempting to delete as many of the contained files and subdirectories
as possible.)
path
- The file or directory to delete.
true
,
otherwise it returns false
.public static boolean deleteRecursively(java.io.File file)
deleteAllRecursively
-methods in this class
that proceed attempting to delete as many of the contained files and subdirectories
as possible.)
file
- The file or directory to delete.
true
,
otherwise it returns false
.public static boolean deleteAllRecursively(java.lang.String path)
deleteRecursively
-methods in this class that
return immediately if an error is encountered).deleteAllRecursively(new File(path), null, null)
.
path
- The file or directory to delete.
true
,
otherwise it returns false
.public static boolean deleteAllRecursively(java.lang.String path, java.util.List<java.io.File> deleted, java.util.List<java.io.File> failed)
deleteRecursively
-methods in this class that
return immediately if an error is encountered).deleted
is null
it is ignored, otherwise all
files that are successfully deteted will be added to the deleted
-list.
If the list failed
is null
it is ignored, otherwise all files that
could not be successfully deteted will be added to the failed
-list.deleteAllRecursively(new File(path), deleted, failed)
.
path
- The file or directory to delete.deleted
- A list to which all successfully deleted files are to be added
(may be null
in which case it is ignored).failed
- A list to which all files that could not be successfully deleted
are to be added (may be null
in which case it is ignored).
true
,
otherwise it returns false
.public static boolean deleteAllRecursively(java.io.File file)
deleteRecursively
-methods in this class that
return immediately if an error is encountered).deleteAllRecursively(file, null, null)
.
file
- The file or directory to delete.
true
,
otherwise it returns false
.public static boolean deleteAllRecursively(java.io.File file, java.util.List<java.io.File> deleted, java.util.List<java.io.File> failed)
deleteRecursively
-methods in this class that
return immediately if an error is encountered).deleted
is null
it is ignored, otherwise all
files that are successfully deteted will be added to the deleted
-list.
If the list failed
is null
it is ignored, otherwise all files
that could not be successfully deteted will be added to the failed
-list.
file
- The file or directory to delete.deleted
- A list to which all successfully deleted files are to be added
(may be null
in which case it is ignored).failed
- A list to which all files that could not be successfully deleted
are to be added (may be null
in which case it is ignored).
true
,
otherwise it returns false
.
|
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |