@MinDoc(copyright="Copyright 2016 A. Weinert", author="Albrecht Weinert", version="V.51", lastModified="7.06.2021", usage="use en lieu de direct java.io.File", purpose="a file with some extra abilities") public final class FileHelper extends Object
File
s to simplify applications that have to handle files. (And
which don't have to?)FileCriteria
,
FileService
Modifier and Type | Class and Description |
---|---|
static class |
FileHelper.OS
Embedded class OS (OutputStream) of FileHelper.
|
Modifier and Type | Field and Description |
---|---|
static FileVisitor |
ASK_OVERWRITE
A FileVisitor default implementation — for asking.
|
static FileVisitor |
askOverwrite
Default setting for procedure to ask on file changes.
|
static Comparator<File> |
comparator
A extensive File comparator.
|
static File[] |
NO_FileX
Array of no Files.
|
static boolean |
sortFNC
Case of file names is relevant for sorting.
|
Modifier and Type | Method and Description |
---|---|
static int |
compare(File d1,
File d2)
Compare two files.
|
static boolean |
contains(File thisFile,
File otherFile)
Ask if other file or directory may be part of this file.
|
static int |
copyFrom(Input ein,
File out,
OutMode outMode)
Write into a file.
|
static String |
getCompleteFilePath(String basePath,
String name)
Construct a complete path.
|
static File |
getInstance(String path)
Factory by pathname (String).
|
static File |
getInstance(String basePath,
String name)
Factory by pathname (String) and path relative name (String).
|
static String |
getType(File f)
Fetching the file type, meaning the name's extension.
|
static String |
getType(String name)
Extracting the file type (extension) from a given file name.
|
static String |
infoLine(File f)
Generate a line for a file list.
|
static StringBuilder |
infoLine(File f,
StringBuilder dest,
boolean noParent,
boolean quoteSpace)
Generate a line for a file list.
|
static StringBuilder |
infoLine(File f,
StringBuilder bastel,
boolean noParent,
boolean quoteSpace,
String relateTo)
Generate a line for a file list.
|
static boolean |
isOfType(File f,
CharSequence types)
Check the file type (i.e. the name's extension).
|
static String |
listLine(File f)
Generate a line for a file list.
|
static FileHelper.OS |
makeOS(File f,
OutMode mode)
Make an output connected to the File.
|
static FileHelper.OS |
makeOS(File f,
OutMode mode,
Charset outEncoding)
Make an output connected to the File given.
|
static StringBuilder |
pathName(File f,
StringBuilder dest,
boolean noParent,
boolean quoteSpace)
Generate a file denomination [Path\]Name.
|
static StringBuilder |
pathName(File f,
StringBuilder dest,
boolean noParent,
boolean quoteSpace,
String relateTo)
Generate a file denomination [Path\]Name.
|
static boolean |
sameDir(File f,
Object o)
Compare two file paths (only).
|
static boolean |
sameName(File f,
Object o)
Compare two file names (only).
|
static boolean |
testDescribe(File f,
PrintWriter out)
Output a test description of this file.
|
static boolean |
wildEqual(File f,
String wildName)
Compare the name with a pattern with optionally wildcards.
|
public static Comparator<File> comparator
compare(File, File)
public static final File[] NO_FileX
public static boolean sortFNC
ComVar.NOT_WINDOWS
; true,
if the platform is definitely not Windows.public static final FileVisitor ASK_OVERWRITE
FileVisitor
by an object of that inetrface's embedded class
FileVisitor.AskConsImpl
with default settings. It is to ask on the
console by nationalised text before writing on existing files.public static volatile FileVisitor askOverwrite
ASK_OVERWRITE
. It may be changed.FileVisitor.AskGrafImpl
.makeOS()
,
ASK_OVERWRITE
public static final int compare(File d1, File d2)
java.util.Comparator
. This method compares
file properties according to their ordering not file content.File
.equals()
.public static final String getCompleteFilePath(String basePath, String name)
basePath
and name
are null, null is
returned.basePath
)
and name
. These may be absolute or relative, containing relative
path expressions like ..\ or .\ . The returned name will be an
unambiguous and complete — "canonical" —) pathname.
A missing separator (\ bei DOS/Windows) at the end of basePath
will be supplied. basePath
if given always denotes
directory.)File.getCanonicalPath()
on Windows now seems to have the same
effect as the (quite older) code here. Be it not for the Exceptions
threatening in File's method File.getCanonicalPath()
(IO and
Security for just a bit file name syntax handling?) the old code here
could be removed.basePath
- the path (directory) (or null)name
- the file namegetInstance()
public static File getInstance(String basePath, String name)
getFileWithCompletePath()
will be applied to basePath.)basePath
- The name of a directory either absolute
or relative to the applications actual pathname
- The name of a file or directory relative to path.public static File getInstance(String path)
getFileWithCompletePath()
will be applied to basePath.)path
- The name of a file or directory either absolute
or relative to the applications actual pathpublic static String getType(String name)
name
contains no dot (.) before the
last character. name
- a "pure" name without any paths or drive lettersgetType(File)
public static String getType(File f)
isOfType()
,
getCompleteFilePath()
public static boolean isOfType(File f, CharSequence types)
types
. The matching allows for semicolon separated
lists as well as wildCards (* ?) in types
and is not case
sensitive.types
null, true is returned.types
has an empty entry (like the second one in
".htm; ;.exe"), than (and only than) a file name
with no extension ("otto") will get a true.types
- the extensions asked forf
- the file to get the type fromgetType()
,
getInstance()
,
getCompleteFilePath()
,
wildEqual()
,
TextHelper.wildEqual(CharSequence, CharSequence, boolean)
public static boolean contains(File thisFile, File otherFile)
File.getPath()
) of the
otherFile
provided contains thisFile
's path really
as a part.otherFile
being a subdirectory of
thisFile
or directly or indirectly contained. It must be
"real containment"; just same paths return false.otherFile
- possibly contained in thisFile
public static boolean wildEqual(File f, String wildName)
wildname
,
that may as well contain wildcard characters (? and *).wildName
- the pattern, may include wildcardsTextHelper.wildEqual(CharSequence, CharSequence, boolean)
,
isOfType(File, CharSequence)
public static boolean testDescribe(File f, PrintWriter out)
out
- the destination for the descriptionlistLine()
public static String listLine(File f)
testDescribe()
,
infoLine(File)
public static StringBuilder pathName(File f, StringBuilder dest, boolean noParent, boolean quoteSpace)
pathName(dest, noParent, quoteSpace, null)
.public static StringBuilder pathName(File f, StringBuilder dest, boolean noParent, boolean quoteSpace, String relateTo)
dest
- the StringBuilder to append to; if null it is made with
starting capacity 89noParent
- true: omit directory informationquoteSpace
- true: If name contains spaces, surround by "relateTo
- if the parent directory starts with relateTo this
starting part will be omittedtestDescribe()
,
listLine()
public static String infoLine(File f)
testDescribe()
,
listLine()
public static StringBuilder infoLine(File f, StringBuilder dest, boolean noParent, boolean quoteSpace)
infoLine(f, dest, noParent, quoteSpace, null)
.public static StringBuilder infoLine(File f, StringBuilder bastel, boolean noParent, boolean quoteSpace, String relateTo)
listLine(File)
.noParent
- true: generate without parent directory informationquoteSpace
- true: If name contains spaces, surround by "relateTo
- if the parent directory starts with relateTo this
starting part will be omittedtestDescribe()
,
listLine(File)
public static FileHelper.OS makeOS(File f, OutMode mode, Charset outEncoding)
FileHelper.OS
and hence FileOutputStream
plus a
PrintWriter
(pw
) contained.OutMode
for the possible
behaviours. null is treated as
OutMode
.ASK
.mode
- how to handle existing filesoutEncoding
- the PrintWriter's encoding; null or defaults to
platform's file encodingFileHelper.OS
for this File
or null if impossible or forbiddenASK_OVERWRITE
,
askOverwrite
,
FileVisitor
,
FileVisitor.AskConsImpl
,
FileVisitor.AskGrafImpl
public static final FileHelper.OS makeOS(File f, OutMode mode)
public static int copyFrom(Input ein, File out, OutMode outMode)
outMode
like described for makeOS()
).OutMode
UPDATE
means: If the
Input
object represents a file or an URL and if the given File
already exists it is overwritten if the given File's last modification is
older than that of the Input
.public static boolean sameName(File f, Object o)