public class FileUtils extends Object
T-Plan Robot Enterprise, (C) 2009-2022 T-Plan Limited. All rights reserved.
Modifier and Type | Field and Description |
---|---|
static Map<File,Boolean> |
canCreateFileMap
Map used to improve performance of the
canCreateNewFile(java.io.File) method. |
Constructor and Description |
---|
FileUtils() |
Modifier and Type | Method and Description |
---|---|
static boolean |
canCreateNewFile(File f)
Test whether the argument file can be created for writing.
|
static boolean |
contains(File dir,
File f)
Find out if a directory is parent of a file meaning that the directory
is either the direct or indirect (more top-level) file parent.
|
static void |
copy(InputStream in,
File out)
Write the input stream to a file.
|
static void |
copy(InputStream in,
OutputStream out,
boolean close)
Write the input stream to the output one.
|
static void |
copy(InputStream in,
OutputStream out,
boolean closeIn,
boolean closeOut)
Write the input stream to the output one.
|
static boolean |
copyDirectoryContent(File sourceDir,
File targetDir,
List<File> exclude,
Map<File,File> changes,
boolean rewrite)
Copy the directory content to another directory.The method is interactive
meaning it displays an option message when the target file exists.
|
static boolean |
copyDirectoryContent(File sourceDir,
File targetDir,
Map<File,File> changes,
boolean rewrite)
Copy the directory content to another directory.
|
static boolean |
copyDirectoryContent(File sourceDir,
File targetDir,
Window owner,
Map<File,File> changes)
Copy the directory content to another directory.
|
static void |
copyFile(File in,
File out)
Copy a file.
|
static void |
delete(File f,
int[] cnt,
boolean dryRun)
Delete the selected file.
|
static void |
deleteEmptyDirs(File dir)
Delete empty directories in the argument dir recursively.
|
static File |
extract(File zipFile,
String pathInZip,
boolean search,
File target)
Extract a file from a ZIP archive.
|
static List<File> |
findFile(File dir,
String name,
List<File> outList,
boolean useRegularExpression,
boolean ignoreCase,
boolean[] stop,
String[] currentDir)
Search for a file path recursively for a file whose name matches a particular string or a
Pattern compliant regular expression. |
static List<File> |
findFile(File dir,
String name,
List<File> outList,
boolean useRegularExpression,
boolean ignoreCase,
boolean[] stop,
String[] currentDir,
VetoableChangeListener listener)
Search for a file path recursively for a file whose name matches a particular string or a
Pattern compliant regular expression. |
static String |
getExtension(File f)
Get the lower case extension of a file.
|
static String |
getExtension(String f)
Get the extension of a file.
|
static File |
getFileFromURL(String link,
StringBuilder frag)
Extract the file part from a URL.
|
static String |
getFullPath(File f)
Get the file full path (exception free).
|
static String |
getInstallPath()
Get the Robot install path, i.e.
|
static String |
getInstallPath(Class clazz)
Get the class (install) path of the file the argument class was loaded
from.
|
static File |
getJarFile()
Get the robot.jar file.
|
static File |
getJarFile(Class clazz)
Get the JAR file the argument class was loaded from.
|
static String |
getJarManifestField(String clazz,
String... fields)
Get a field from a manifest file of a JAR file.
|
static String |
getRelativePath(File file,
File relativeTo)
Get relative path to a file resolved to another file or directory.
|
static boolean |
isLink(File file)
Check if the argument is a link.
|
static boolean |
isParent(File f,
File parentFile)
Find out if a directory is parent of a file meaning that the directory
is either the direct or indirect (more top-level) file parent.
|
static boolean |
moveOrCopy(Component owner,
File[] files,
File targetPath,
boolean copy,
boolean interactive,
boolean treatAsOne,
Map<File,File> changes)
Move or copy the specified files and/or directories to the specified
target path.
|
static File |
parseFile(String file)
Create a file from a String.
|
static StringBuilder |
read(File f,
Charset encoding)
Read a text file into a string builder.Suitable for small plain text
files.
|
static StringBuilder |
read(InputStream is,
Charset encoding)
Read an input stream into a string builder.
|
static boolean |
rename(Component owner,
File source,
File target,
boolean copy,
boolean interactive,
Map<File,File> changes)
Rename (move) or copy the specified file (directory) to the specified
target file (directory).
|
static void |
write(File f,
String content)
Create a file with the specified text content.
|
static void |
write(File f,
String content,
String encoding)
Create a file with the specified text content.
|
static void |
writeSafely(File f,
String content,
String encoding)
Create a file with the specified text content.
|
public static Map<File,Boolean> canCreateFileMap
canCreateNewFile(java.io.File)
method.public static boolean moveOrCopy(Component owner, File[] files, File targetPath, boolean copy, boolean interactive, boolean treatAsOne, Map<File,File> changes)
owner
- the owning window. It will be used as a parent of the
question and error messages.files
- the files to copy/move.targetPath
- the target path to copy/move the files to.copy
- true performs a copy, false a move.interactive
- true will enable the error messages, false will fall
back to defaults (overwrite all, restore on an error).treatAsOne
- true will treat the file list as a single file in terms
of the overwrite questions. This allows to move multiple files such as
for example a template image and its meta data file in a way that the
user thinks he/she is handling just one file.changes
- optional map for the performed changes ([newFile, oldFile]
pairs).public static boolean rename(Component owner, File source, File target, boolean copy, boolean interactive, Map<File,File> changes)
owner
- the owning window. It will be used as a parent of the
question and error messages.source
- the file to copy/move.target
- the target path to copy/move the file to.copy
- true performs a copy, false a rename (move).interactive
- true will enable the error messages, false will fall
back to defaults (overwrite all, restore on an error).changes
- optional map for the performed changes ([newFile, oldFile]
pairs).public static void delete(File f, int[] cnt, boolean dryRun) throws IOException
f
- the file to delete.cnt
- optional int array to store the number of deleted files to.
It may be used by an asynchronous process to report the progress.dryRun
- the value of true will run through the file(s) but it will
not delete the files. This mode can be used together with the cnt
counter to count the number of files that can be deleted.IOException
- when a file/directory fails to delete.public static void copyFile(File in, File out) throws IOException
in
- the input file.out
- the output file.IOException
- on an I/O error.public static boolean copyDirectoryContent(File sourceDir, File targetDir, Window owner, Map<File,File> changes) throws Exception
moveOrCopy(java.awt.Component, java.io.File[], java.io.File, boolean, boolean, boolean, java.util.Map)
method
this one doesn't resolve the conflicts up front and copies the files one by
one directly. If the action gets canceled by the user the files copied so far
are not removed.sourceDir
- the source directory.targetDir
- the target directory.owner
- the window owner (used as a parent of the messages).changes
- an optional output map of [sourceFile, targetFile] pairs to
capture the files that were copied successfully..Exception
- on an I/O error.public static boolean copyDirectoryContent(File sourceDir, File targetDir, Map<File,File> changes, boolean rewrite) throws Exception
moveOrCopy(java.awt.Component, java.io.File[], java.io.File, boolean, boolean, boolean, java.util.Map)
method
this one doesn't resolve the conflicts up front and copies the files one by
one directly. If the action gets canceled by the user the files copied so far
are not removed.sourceDir
- the source directory.targetDir
- the target directory.changes
- an optional output map of [sourceFile, targetFile] pairs to
capture the files that were copied successfully..rewrite
- true to force rewrite the files or false to skip.Exception
- on an I/O error.public static boolean copyDirectoryContent(File sourceDir, File targetDir, List<File> exclude, Map<File,File> changes, boolean rewrite) throws Exception
moveOrCopy(java.awt.Component, java.io.File[], java.io.File, boolean, boolean, boolean, java.util.Map)
method this one doesn't resolve the conflicts up front and copies the
files one by one directly. If the action gets canceled by the user the
files copied so far are not removed.sourceDir
- the source directory.targetDir
- the target directory.exclude
- the list of files or directories to be excluded (skipped).changes
- an optional output map of [sourceFile, targetFile] pairs
to capture the files that were copied successfully..rewrite
- true to force rewrite the files or false to skip.Exception
- on an I/O error.public static void copy(InputStream in, File out) throws IOException
in
- an input stream.out
- the output file.IOException
- on an I/O error.public static void copy(InputStream in, OutputStream out, boolean close) throws IOException
in
- the input stream.out
- the output stream.close
- true will close the output stream after the data transfer
is completed or an I/O error is thrown. The input stream gets closed
always regardless of this flag.IOException
- on an I/O error.public static void copy(InputStream in, OutputStream out, boolean closeIn, boolean closeOut) throws IOException
in
- the input stream.out
- the output stream.closeIn
- true will close the input stream after the data transfer
is completed or an I/O error is thrown.closeOut
- true will close the output stream after the data transfer
is completed or an I/O error is thrown.IOException
- on an I/O error.public static String getExtension(File f)
f
- a file.public static String getExtension(String f)
f
- a file.public static String getFullPath(File f)
File.getCanonicalPath()
and falls back to the absolute one (File.getAbsolutePath()
) when
the resolution fails.f
- a file.public static String getRelativePath(File file, File relativeTo) throws IOException
file
- file to get the relative path for.relativeTo
- file or directory to resolve against.IOException
- when the File.getCanonicalPath() method throws an I/O exception.public static boolean contains(File dir, File f)
dir
- a directory.f
- a file.public static boolean isParent(File f, File parentFile)
f
- a file.parentFile
- the file parent.parentFile
is parent of f
.public static boolean canCreateNewFile(File f) throws IOException
Test whether the argument file can be created for writing. If the file
already exists, the method returns result of the File.canWrite()
method.
If the file doesn't exist, the method makes an attempt to create it together
with any necessary directories to test whether it is possible or not. Any
newly created objects are deleted in the end of the test.
Note that for performance reasons the files tested by this method are
cached together with the result. If the method is called repeatedly with
the same file, the file is not really tested in the file system and the
cached result is returned instead. Should you need to clear the cache, see
the canCreateFileMap
map. To disable caching simply set the map
to null; to reenable it assign it a new hash map.
f
- a file.IOException
- if any of the directory or file create operations throw
an IO exception, it is rethrown.public static List<File> findFile(File dir, String name, List<File> outList, boolean useRegularExpression, boolean ignoreCase, boolean[] stop, String[] currentDir, VetoableChangeListener listener)
Pattern
compliant regular expression.dir
- root directory to start the search from.name
- the name or regular expression to match.outList
- output list for the located files. A new one will be
created and returned when it is null.useRegularExpression
- true will handle the name
as a
regular expression. If the argument is false, a regular string comparison
will be done.ignoreCase
- only used when useRegularExpression=false
.
If this flag is true, the file name will be compared to the string specified
by name
using String.equalsIgnoreCase(java.lang.String)
rather than String.equals(java.lang.Object)
.stop
- if a non-null boolean array with the length>=1 is
specified, the value of stop[0]
will be periodically checked
and when it becomes "true", the file search will stop immediately. This
functionality may be used for example for a "Stop search" button in the GUI.currentDir
- if a non-null String array with the length>=1 is
specified, the file search will populate currentDir[0]
with
the absolute directory path whenever a new directory is entered. This
functionality may be used for example for a GUI component which shows the
currently processed folder.listener
- an optional listener to be called whenever a matching
file is discovered. The property event will have the name set to "file",
the old value will be null and the new value will be set to the file
(File
instance). If the listener throws a PropertyVetoException
the search will continue but the file will not be added to the returned list.public static List<File> findFile(File dir, String name, List<File> outList, boolean useRegularExpression, boolean ignoreCase, boolean[] stop, String[] currentDir)
Pattern
compliant regular expression.dir
- root directory to start the search from.name
- the name or regular expression to match.outList
- output list for the located files. A new one will be
created and returned when it is null.useRegularExpression
- true will handle the name
as a
regular expression. If the argument is false, a regular string comparison
will be done.ignoreCase
- only used when useRegularExpression=false
.
If this flag is true, the file name will be compared to the string specified
by name
using String.equalsIgnoreCase(java.lang.String)
rather than String.equals(java.lang.Object)
.stop
- if a non-null boolean array with the length>=1 is
specified, the value of stop[0]
will be periodically checked
and when it becomes "true", the file search will stop immediately. This
functionality may be used for example for a "Stop search" button in the GUI.currentDir
- if a non-null String array with the length>=1 is
specified, the file search will populate currentDir[0]
with
the absolute directory path whenever a new directory is entered. This
functionality may be used for example for a GUI component which shows the
currently processed folder..public static boolean isLink(File file)
file
- a file.public static String getInstallPath()
public static String getInstallPath(Class clazz)
clazz
- a class.public static File getJarFile()
public static File getJarFile(Class clazz)
clazz
- a class.public static String getJarManifestField(String clazz, String... fields)
clazz
- a class loaded from the JAR file.fields
- one or more fields to look for ordered by the priority (if
the first one is not found look for the second one etc.).public static File getFileFromURL(String link, StringBuilder frag)
link
- a URL.frag
- an optional buffer to write the fragment to (the anchor after
the '#' sign).public static StringBuilder read(File f, Charset encoding) throws IOException
f
- a file to read.encoding
- file encoding. If null the method will fall back to the
default OS one.IOException
- on an I/O error.public static StringBuilder read(InputStream is, Charset encoding) throws IOException
is
- an input stream to read.encoding
- file encoding. If null the method will fall back to the
default OS one.IOException
- on an I/O error.public static void write(File f, String content) throws IOException
f
- a file to create.content
- the text to write to the file.IOException
- on an I/O error.public static void write(File f, String content, String encoding) throws IOException
f
- a file to create.content
- the text to write to the file.encoding
- file encodingIOException
- on an I/O error.public static void writeSafely(File f, String content, String encoding) throws IOException
f
- a file to create.content
- the text to write to the file.encoding
- file encodingIOException
- on an I/O error.public static File extract(File zipFile, String pathInZip, boolean search, File target) throws FileNotFoundException, IOException
zipFile
- a ZIP or JAR file.pathInZip
- path to the file in the archive to be extracted.search
- true will make an attempt to find the first file of the
same name.target
- where to extract the file to.FileNotFoundException
- when the ZIP file is not found.IOException
- when the ZIP file can not be read or the the method
can't write to the target file.public static File parseFile(String file)
file
- a file path.public static void deleteEmptyDirs(File dir)
dir
- a directory.