|
intarsys runtime library | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.intarsys.tools.file.FileTools
public class FileTools
Some utility methods to ease life with File
instances.
Nested Class Summary | |
---|---|
static class |
FileTools.Lock
|
Field Summary | |
---|---|
static String |
DIRECTORY_LOCK
|
Method Summary | |
---|---|
static void |
appendFile(File source,
File destination)
Concatenate the two files given in source and
destination . |
static File |
checkDirectory(File dir,
boolean create,
boolean checkCanRead,
boolean checkCanWrite)
Utility method for checking the availablity of a directory. |
static File |
checkDirectory(String path,
boolean create,
boolean checkCanRead,
boolean checkCanWrite)
|
static void |
copyBinaryFile(File source,
File destination)
Copy the byte content of source to destination . |
static void |
copyFile(File source,
File destination)
|
static void |
copyFile(File source,
String sourceEncoding,
File destination,
String destinationEncoding)
Copy the character content of source to
destination . |
static void |
copyRecursively(File source,
File destination)
|
static File |
copyRecursivelyInto(File source,
File destinationParent,
String newName)
|
static void |
createEmptyFile(File file)
Create an empty file. |
static File |
createTempFile(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 File |
createTempFile(String fileName)
Create a file object representing a temporary file in the user's temp dir with the given filename. |
static void |
deleteAfter(File directory,
long millis,
boolean recursiveScan)
Delete any file in directory that is older than
millis milliseconds. |
static boolean |
deleteRecursivly(File file)
Deletes a file or directory, if necessary recursivly. |
static boolean |
deleteRecursivly(File file,
boolean deleteRoot)
Deletes a file or directory, if necessary recursivly. |
static boolean |
equalsOnSystem(File source,
File destination)
true when the two files represent the same physical file in
the file system. |
static String |
getBaseName(File file)
Get the local name of the file in its directory without the extension. |
static String |
getBaseName(File file,
String defaultName)
Get the local name of the file in its directory without the extension. |
static String |
getBaseName(String filename)
Get the local name of the file in its directory without the extension. |
static String |
getBaseName(String filename,
String defaultName)
Get the local name of the file in its directory without the extension. |
static String |
getEncoding()
|
static String |
getExtension(File file)
Get the extension of the file name. |
static String |
getExtension(String filename)
Get the extension of the file name. |
static String |
getExtension(String filename,
String defaultName)
Get the extension of the file name. |
static File |
getParentFile(File file)
Try to get a valid parent for file. |
static String |
getPathRelativeTo(File file,
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) == ../.. |
static boolean |
isAncestor(File parent,
File descendant)
|
static boolean |
isWindows()
|
static FileTools.Lock |
lock(File file)
|
static void |
renameFile(File source,
File destination)
|
static void |
renameFile(File source,
String sourceEncoding,
File destination,
String destinationEncoding)
"Rename" a file. |
static File |
resolvePath(File parent,
String path)
Return a new File instance for "path". |
static byte[] |
toBytes(File file)
Create a byte array with the files content. |
static String |
toString(File file)
Read a file's content at once and return as a string. |
static String |
toString(File file,
String encoding)
Read a file's content at once and return as a string in the correct encoding. |
static String |
trimPath(String param)
Replaces all characters that are generally not allowed or considered useful in filenames with underscore. |
static void |
wait(File file,
long timeout,
long delay)
Wait for a file to arrive. |
static void |
write(File file,
byte[] bytes)
Create a file from the byte content. |
static void |
write(File file,
String text)
Create a file from the string content. |
static void |
write(File file,
String text,
boolean append)
Create a file from the string content. |
static void |
write(File file,
String text,
String encoding)
Create a file from the string content. |
static void |
write(File file,
String text,
String encoding,
boolean append)
Create a file from the string content / append a string to a file |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String DIRECTORY_LOCK
Method Detail |
---|
public static void appendFile(File source, File destination) throws IOException
source
and
destination
.
source
- The file to be appended.destination
- The file to append to.
IOException
public static File checkDirectory(File dir, boolean create, boolean checkCanRead, boolean checkCanWrite) throws 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.
IOException
public static File checkDirectory(String path, boolean create, boolean checkCanRead, boolean checkCanWrite) throws IOException
IOException
checkDirectory(File, boolean, boolean, boolean)
public static void copyBinaryFile(File source, File destination) throws IOException
source
to destination
.
source
- The file whose contents we should copy.destination
- The file where the contents are copied to.
IOException
public static void copyFile(File source, File destination) throws IOException
IOException
copyBinaryFile(File, File)
public static void copyFile(File source, String sourceEncoding, File destination, String destinationEncoding) throws 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.
IOException
public static void copyRecursively(File source, File destination) throws IOException
IOException
public static File copyRecursivelyInto(File source, File destinationParent, String newName) throws IOException
IOException
public static void createEmptyFile(File file) throws IOException
file
-
IOException
public static File createTempFile(File file) throws IOException
file
- filename to use
IOException
public static File createTempFile(String fileName) throws IOException
This does not actually create a file in the file system.
filename
- filename to use
IOException
public static void deleteAfter(File directory, long millis, boolean recursiveScan) throws 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.
IOException
public static boolean deleteRecursivly(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(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(File source, 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 String getBaseName(File file)
file
- The file whose base name is requested.
public static String getBaseName(File file, String defaultName)
file
- The file whose base name is requested.
public static String getBaseName(String filename)
filename
- The filename whose base name is requested.
public static String getBaseName(String filename, String defaultName)
filename
- The filename whose base name is requested.defaultName
- returned if filename is null or a empty String
public static String getEncoding()
public static String getExtension(File file)
file
- The file whose extension is requested.
public static String getExtension(String filename)
filename
- The filename whose extension is requested.
public static String getExtension(String filename, String defaultName)
filename
- The filename whose extension is requested.defaultName
- returned if the filename is empty or null or there is no
extension
public static File getParentFile(File file)
file
- public static String getPathRelativeTo(File file, File base) throws IOException
base = /a/b/c;
file = /a/d/e/x.txt;
getRelativePath(file, base) == ../../d/e/x.txt;
home
- base path, should be a directory, not a file, or it doesn't
make sensef
- file to generate path for
IOException
public static boolean isAncestor(File parent, File descendant)
public static boolean isWindows()
public static FileTools.Lock lock(File file)
public static void renameFile(File source, File destination) throws IOException
IOException
renameFile(File, String, File, String)
public static void renameFile(File source, String sourceEncoding, File destination, String destinationEncoding) throws 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.
IOException
public static File resolvePath(File parent, 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(File file) throws IOException
file
- The file to read.
IOException
public static String toString(File file) throws IOException
Use with care!
file
- The file to read.
IOException
public static String toString(File file, String encoding) throws IOException
Use with care!
file
- The file to read.encoding
- The encoding to use.
IOException
public static String trimPath(String param)
param
- java.lang.String
public static void wait(File file, long timeout, long delay) throws 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.
IOException
public static void write(File file, byte[] bytes) throws IOException
file
- The file to write/createtext
- The text to be written into the file.
IOException
public static void write(File file, String text) throws IOException
file
- The file to write/createtext
- The text to be written into the file.
IOException
public static void write(File file, String text, boolean append) throws 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.
IOException
public static void write(File file, String text, String encoding) throws IOException
file
- The file to write/createtext
- The text to be written into the file.
IOException
public static void write(File file, String text, String encoding, boolean append) throws 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.
IOException
|
intarsys runtime library | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |