de.intarsys.tools.file
public class FileTools extends java.lang.Object
File
instances.Modifier and Type | Class and Description |
---|---|
static class |
FileTools.Lock |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DIRECTORY_LOCK |
Modifier and Type | Method and Description |
---|---|
static void |
appendFile(java.io.File source,
java.io.File destination)
Concatenate the two files given in
source and
destination . |
static java.io.File |
checkDirectory(java.io.File dir,
boolean create,
boolean checkCanRead,
boolean checkCanWrite)
Utility method for checking the availablity of a directory.
|
static java.io.File |
checkDirectory(java.lang.String path,
boolean create,
boolean checkCanRead,
boolean checkCanWrite) |
static void |
copyBinaryFile(java.io.File source,
java.io.File destination)
Copy the byte content of
source to destination . |
static void |
copyFile(java.io.File source,
java.io.File destination) |
static void |
copyFile(java.io.File source,
java.lang.String sourceEncoding,
java.io.File destination,
java.lang.String destinationEncoding)
Copy the character content of
source to
destination . |
static void |
copyRecursively(java.io.File source,
java.io.File destination) |
static java.io.File |
copyRecursivelyInto(java.io.File source,
java.io.File destinationParent,
java.lang.String newName) |
static void |
createEmptyFile(java.io.File file)
Create an empty file.
|
static java.io.File |
createTempFile(java.io.File file)
Create a file object representing a temporary file in the user's temp dir
with the same name as the given file.
|
static java.io.File |
createTempFile(java.lang.String filename)
Create a file object representing a temporary file in the user's temp dir
with the given filename.
|
static void |
deleteAfter(java.io.File directory,
long millis,
boolean recursiveScan)
Delete any file in
directory that is older than
millis milliseconds. |
static boolean |
deleteRecursivly(java.io.File file)
Deletes a file or directory, if necessary recursivly.
|
static boolean |
deleteRecursivly(java.io.File file,
boolean deleteRoot)
Deletes a file or directory, if necessary recursivly.
|
static boolean |
equalsOnSystem(java.io.File source,
java.io.File destination)
true when the two files represent the same physical file in
the file system. |
static java.lang.String |
getBaseName(java.io.File file)
Get the local name of the file in its directory without the extension.
|
static java.lang.String |
getBaseName(java.io.File file,
java.lang.String defaultName)
Get the local name of the file in its directory without the extension.
|
static java.lang.String |
getBaseName(java.lang.String filename)
Get the local name of the file in its directory without the extension.
|
static java.lang.String |
getBaseName(java.lang.String filename,
java.lang.String defaultName)
Get the local name of the file in its directory without the extension.
|
static java.lang.String |
getEncoding() |
static java.lang.String |
getExtension(java.io.File file)
Get the extension of the file name.
|
static java.lang.String |
getExtension(java.lang.String filename)
Get the extension of the file name.
|
static java.lang.String |
getExtension(java.lang.String filename,
java.lang.String defaultName)
Get the extension of the file name.
|
static java.lang.String |
getFileName(java.lang.String filename)
Get the local name of the file in its directory (with extension).
|
static java.lang.String |
getFileName(java.lang.String filename,
java.lang.String defaultName)
Get the local name of the file in its directory (with extension).
|
static java.io.File |
getParentFile(java.io.File file)
Try to get a valid parent for file.
|
static java.lang.String |
getPathRelativeTo(java.io.File file,
java.io.File base)
get relative path of "file" with respect to "base" directory example :
base = /a/b/c;
file = /a/d/e/x.txt;
getRelativePath(file, base) == ../../d/e/x.txt;
|
static java.lang.String |
getPathRelativeTo(java.io.File file,
java.io.File base,
boolean ifAncestor) |
static boolean |
isAncestor(java.io.File parent,
java.io.File descendant) |
static boolean |
isWindows() |
static FileTools.Lock |
lock(java.io.File file) |
static void |
renameFile(java.io.File source,
java.io.File destination) |
static void |
renameFile(java.io.File source,
java.lang.String sourceEncoding,
java.io.File destination,
java.lang.String destinationEncoding)
"Rename" a file.
|
static java.io.File |
resolvePath(java.io.File parent,
java.lang.String path)
Return a new
File instance for "path". |
static byte[] |
toBytes(java.io.File file)
Create a byte array with the files content.
|
static java.lang.String |
toString(java.io.File file)
Read a file's content at once and return as a string.
|
static java.lang.String |
toString(java.io.File file,
java.lang.String encoding)
Read a file's content at once and return as a string in the correct
encoding.
|
static java.lang.String |
trimPath(java.lang.String param)
Replaces all characters that are generally not allowed or considered
useful in filenames with underscore.
|
protected static void |
unlock(FileTools.Lock lock) |
static void |
wait(java.io.File file,
long timeout,
long delay)
Wait for a file to arrive.
|
static void |
write(java.io.File file,
byte[] bytes)
Create a file from the byte content.
|
static void |
write(java.io.File file,
java.lang.String text)
Create a file from the string content.
|
static void |
write(java.io.File file,
java.lang.String text,
boolean append)
Create a file from the string content.
|
static void |
write(java.io.File file,
java.lang.String text,
java.lang.String encoding)
Create a file from the string content.
|
static void |
write(java.io.File file,
java.lang.String text,
java.lang.String encoding,
boolean append)
Create a file from the string content / append a string to a file
|
public static final java.lang.String DIRECTORY_LOCK
public static void appendFile(java.io.File source, java.io.File destination) throws java.io.IOException
source
and
destination
.source
- The file to be appended.destination
- The file to append to.java.io.IOException
public static java.io.File checkDirectory(java.io.File dir, boolean create, boolean checkCanRead, boolean checkCanWrite) throws java.io.IOException
dir
- The directory to check.create
- Flag if we should create if dir not already exists.checkCanRead
- Flag if we should check read permission.checkCanWrite
- Flag if we should check write permission.java.io.IOException
public static java.io.File checkDirectory(java.lang.String path, boolean create, boolean checkCanRead, boolean checkCanWrite) throws java.io.IOException
java.io.IOException
checkDirectory(File, boolean, boolean, boolean)
public static void copyBinaryFile(java.io.File source, java.io.File destination) throws java.io.IOException
source
to destination
.source
- The file whose contents we should copy.destination
- The file where the contents are copied to.java.io.IOException
public static void copyFile(java.io.File source, java.io.File destination) throws java.io.IOException
java.io.IOException
copyBinaryFile(File, File)
public static void copyFile(java.io.File source, java.lang.String sourceEncoding, java.io.File destination, java.lang.String destinationEncoding) throws java.io.IOException
source
to
destination
.source
- The file whose contents we should copy.sourceEncoding
- The encoding of the source byte stream.destination
- The file where the contents are copied to.destinationEncoding
- The encoding of the destination byte stream.java.io.IOException
public static void copyRecursively(java.io.File source, java.io.File destination) throws java.io.IOException
java.io.IOException
public static java.io.File copyRecursivelyInto(java.io.File source, java.io.File destinationParent, java.lang.String newName) throws java.io.IOException
java.io.IOException
public static void createEmptyFile(java.io.File file) throws java.io.IOException
file
- java.io.IOException
public static java.io.File createTempFile(java.io.File file) throws java.io.IOException
file
- file to usejava.io.IOException
public static java.io.File createTempFile(java.lang.String filename) throws java.io.IOException
This does not actually create a file in the file system.
filename
- filename to usejava.io.IOException
public static void deleteAfter(java.io.File directory, long millis, boolean recursiveScan) throws java.io.IOException
directory
that is older than
millis
milliseconds. When recursiveScan
is
true
the directory lookup is made recursive.directory
- The directory to scan.millis
- The number of milliseconds a file is allowed to live.recursiveScan
- Flag if we should handle directories recursive.java.io.IOException
public static boolean deleteRecursivly(java.io.File file)
Returns true
if file could be deleted inclusive its
components, otherwise false.
file
- The file or directory to delete.true
if file could be deleted inclusive its
components, otherwise false.public static boolean deleteRecursivly(java.io.File file, boolean deleteRoot)
Returns true
if file could be deleted inclusive its
components, otherwise false.
file
- The file or directory to delete.deleteRoot
- Flag if the root directory should be deleted itself.true
if file could be deleted inclusive its
components, otherwise false.public static boolean equalsOnSystem(java.io.File source, java.io.File destination)
true
when the two files represent the same physical file in
the file system.source
- The first file to be checked.destination
- The second file to be checked.true
when the two files represent the same physical
file in the file system.public static java.lang.String getBaseName(java.io.File file)
file
- The file whose base name is requested.public static java.lang.String getBaseName(java.io.File file, java.lang.String defaultName)
file
- The file whose base name is requested.public static java.lang.String getBaseName(java.lang.String filename)
filename
- The filename whose base name is requested.public static java.lang.String getBaseName(java.lang.String filename, java.lang.String defaultName)
filename
- The filename whose base name is requested.defaultName
- returned if filename is null or a empty Stringpublic static java.lang.String getEncoding()
public static java.lang.String getExtension(java.io.File file)
file
- The file whose extension is requested.public static java.lang.String getExtension(java.lang.String filename)
filename
- The filename whose extension is requested.public static java.lang.String getExtension(java.lang.String filename, java.lang.String defaultName)
filename
- The filename whose extension is requested.defaultName
- returned if the filename is empty or null or there is no
extensionpublic static java.lang.String getFileName(java.lang.String filename)
filename
- The filename whose name is requested.public static java.lang.String getFileName(java.lang.String filename, java.lang.String defaultName)
filename
- The filename whose name is requested.defaultName
- returned if filename is null or a empty Stringpublic static java.io.File getParentFile(java.io.File file)
file
- public static java.lang.String getPathRelativeTo(java.io.File file, java.io.File base) throws java.io.IOException
base = /a/b/c;
file = /a/d/e/x.txt;
getRelativePath(file, base) == ../../d/e/x.txt;
base
- base path, should be a directory, not a file, or it doesn't
make sensefile
- file to generate path forjava.io.IOException
public static java.lang.String getPathRelativeTo(java.io.File file, java.io.File base, boolean ifAncestor)
public static boolean isAncestor(java.io.File parent, java.io.File descendant)
public static boolean isWindows()
public static FileTools.Lock lock(java.io.File file)
public static void renameFile(java.io.File source, java.io.File destination) throws java.io.IOException
java.io.IOException
renameFile(File, String, File, String)
public static void renameFile(java.io.File source, java.lang.String sourceEncoding, java.io.File destination, java.lang.String destinationEncoding) throws java.io.IOException
The effect is that there is a new file destination
, encoded
in destinationEncoding
, the old file source
is
deleted.
source
- The source name of the file.sourceEncoding
- The encoding of the source file.destination
- The destination name of the file.destinationEncoding
- The encoding of the destination file.java.io.IOException
public static java.io.File resolvePath(java.io.File parent, java.lang.String path)
File
instance for "path". If path is relative, than
it will be interpreted as a child of "parent", if it is absolute, it is
returned as is.
ATTENTION: On windows, if "path" is absolute but without drive or UNC prefix, this root information is NOT taken from "parent".
parent
- path
- File
instance for "path".public static byte[] toBytes(java.io.File file) throws java.io.IOException
file
- The file to read.java.io.IOException
public static java.lang.String toString(java.io.File file) throws java.io.IOException
Use with care!
file
- The file to read.java.io.IOException
public static java.lang.String toString(java.io.File file, java.lang.String encoding) throws java.io.IOException
Use with care!
file
- The file to read.encoding
- The encoding to use.java.io.IOException
public static java.lang.String trimPath(java.lang.String param)
param
- java.lang.Stringprotected static void unlock(FileTools.Lock lock)
public static void wait(java.io.File file, long timeout, long delay) throws java.io.IOException
The method waits at most timeout
milliseconds for a file to
arrive. When delay
is != 0 the method checks the file's size
for changes it reaches a stable size.
file
- The file to wait for.timeout
- The maximum time in milliseconds to wait for first occurence
of file
.delay
- The number of milliseconds between two checks against the
files size.java.io.IOException
public static void write(java.io.File file, byte[] bytes) throws java.io.IOException
file
- The file to write/createbytes
- The data to be written into the file.java.io.IOException
public static void write(java.io.File file, java.lang.String text) throws java.io.IOException
file
- The file to write/createtext
- The text to be written into the file.java.io.IOException
public static void write(java.io.File file, java.lang.String text, boolean append) throws java.io.IOException
file
- The file to write/createtext
- The text to be written into the file.append
- Flag to append to an existing file or create a new file.java.io.IOException
public static void write(java.io.File file, java.lang.String text, java.lang.String encoding) throws java.io.IOException
file
- The file to write/createtext
- The text to be written into the file.java.io.IOException
public static void write(java.io.File file, java.lang.String text, java.lang.String encoding, boolean append) throws java.io.IOException
file
- The file to write/createtext
- The text to be written into the file.append
- Flag to append to an existing file or create a new file.java.io.IOException