Package de.intarsys.tools.file
Class FileTools
java.lang.Object
de.intarsys.tools.file.FileTools
Some utility methods to ease life with
File
instances.-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic void
appendFile
(File source, File destination) Concatenate the two files given insource
anddestination
.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 ofsource
todestination
.static void
static void
Copy the character content ofsource
todestination
.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 indirectory
that is older thanmillis
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
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 String
getFileName
(String filename) Get the local name of the file in its directory (with extension).static String
getFileName
(String filename, String defaultName) Get the local name of the file in its directory (with extension).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) == ../../d/e/x.txt;
static String
getPathRelativeTo
(File file, File base, boolean ifAncestor) static boolean
isAncestor
(File parent, File descendant) static boolean
static FileTools.Lock
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 newFile
instance for "path".static byte[]
Create a byte array with the files content.static String
Read a file's content at once and return as a string.static String
Read a file's content at once and return as a string in the correct encoding.static String
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 for a file to arrive.static void
Create a file from the byte content.static void
Create a file from the string content.static void
Create a file from the string content.static void
Create a file from the string content.static void
Create a file from the string content / append a string to a file
-
Field Details
-
DIRECTORY_LOCK
- See Also:
-
-
Method Details
-
appendFile
Concatenate the two files given insource
anddestination
.- Parameters:
source
- The file to be appended.destination
- The file to append to.- Throws:
IOException
-
checkDirectory
public static File checkDirectory(File dir, boolean create, boolean checkCanRead, boolean checkCanWrite) throws IOException Utility method for checking the availablity of a directory.- Parameters:
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.- Returns:
- The checked directory.
- Throws:
IOException
-
checkDirectory
public static File checkDirectory(String path, boolean create, boolean checkCanRead, boolean checkCanWrite) throws IOException - Throws:
IOException
- See Also:
-
copyBinaryFile
Copy the byte content ofsource
todestination
.- Parameters:
source
- The file whose contents we should copy.destination
- The file where the contents are copied to.- Throws:
IOException
-
copyFile
- Throws:
IOException
- See Also:
-
copyFile
public static void copyFile(File source, String sourceEncoding, File destination, String destinationEncoding) throws IOException Copy the character content ofsource
todestination
.- Parameters:
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.- Throws:
IOException
-
copyRecursively
- Throws:
IOException
-
copyRecursivelyInto
public static File copyRecursivelyInto(File source, File destinationParent, String newName) throws IOException - Throws:
IOException
-
createEmptyFile
Create an empty file.- Parameters:
file
-- Throws:
IOException
-
createTempFile
Create a file object representing a temporary file in the user's temp dir with the same name as the given file.- Parameters:
file
- file to use- Returns:
- file object representing a temporary file
- Throws:
IOException
-
createTempFile
Create a file object representing a temporary file in the user's temp dir with the given filename.This does not actually create a file in the file system.
- Parameters:
filename
- filename to use- Returns:
- file object representing a temporary file
- Throws:
IOException
-
deleteAfter
public static void deleteAfter(File directory, long millis, boolean recursiveScan) throws IOException Delete any file indirectory
that is older thanmillis
milliseconds. WhenrecursiveScan
istrue
the directory lookup is made recursive.- Parameters:
directory
- The directory to scan.millis
- The number of milliseconds a file is allowed to live.recursiveScan
- Flag if we should handle directories recursive.- Throws:
IOException
-
deleteRecursivly
Deletes a file or directory, if necessary recursivly.Returns
true
if file could be deleted inclusive its components, otherwise false.- Parameters:
file
- The file or directory to delete.- Returns:
true
if file could be deleted inclusive its components, otherwise false.
-
deleteRecursivly
Deletes a file or directory, if necessary recursivly.Returns
true
if file could be deleted inclusive its components, otherwise false.- Parameters:
file
- The file or directory to delete.deleteRoot
- Flag if the root directory should be deleted itself.- Returns:
true
if file could be deleted inclusive its components, otherwise false.
-
equalsOnSystem
true
when the two files represent the same physical file in the file system.- Parameters:
source
- The first file to be checked.destination
- The second file to be checked.- Returns:
true
when the two files represent the same physical file in the file system.
-
getBaseName
Get the local name of the file in its directory without the extension.- Parameters:
file
- The file whose base name is requested.- Returns:
- The local name of the file in its directory without the extension.
-
getBaseName
Get the local name of the file in its directory without the extension.- Parameters:
file
- The file whose base name is requested.- Returns:
- The local name of the file in its directory without the extension.
-
getBaseName
Get the local name of the file in its directory without the extension.- Parameters:
filename
- The filename whose base name is requested.- Returns:
- The local name of the file in its directory without the extension.
-
getBaseName
Get the local name of the file in its directory without the extension.- Parameters:
filename
- The filename whose base name is requested.defaultName
- returned if filename is null or a empty String- Returns:
- The local name of the file in its directory without the extension.
-
getEncoding
-
getExtension
Get the extension of the file name. If no extension is present, the empty string is returned.- Parameters:
file
- The file whose extension is requested.- Returns:
- The extension of the file name. If no extension is present, the empty string is returned.
-
getExtension
Get the extension of the file name. If no extension is present, the empty string is returned.- Parameters:
filename
- The filename whose extension is requested.- Returns:
- The extension of the file name. If no extension is present, the empty string is returned.
-
getExtension
Get the extension of the file name. If no extension is present, the defaultName is returned.- Parameters:
filename
- The filename whose extension is requested.defaultName
- returned if the filename is empty or null or there is no extension- Returns:
- The extension of the file name. If no extension is present, the empty string is returned.
-
getFileName
Get the local name of the file in its directory (with extension).- Parameters:
filename
- The filename whose name is requested.- Returns:
- The local name of the file in its directory (with extension)
-
getFileName
Get the local name of the file in its directory (with extension).- Parameters:
filename
- The filename whose name is requested.defaultName
- returned if filename is null or a empty String- Returns:
- The local name of the file in its directory (with extension)
-
getParentFile
Try to get a valid parent for file.- Parameters:
file
-
-
getPathRelativeTo
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;
- Parameters:
file
- file to generate path forbase
- base path, should be a directory, not a file, or it doesn't make sense- Returns:
- path from home to f as a string
- Throws:
IOException
-
getPathRelativeTo
-
isAncestor
-
isWindows
public static boolean isWindows() -
lock
-
renameFile
- Throws:
IOException
- See Also:
-
renameFile
public static void renameFile(File source, String sourceEncoding, File destination, String destinationEncoding) throws IOException "Rename" a file.The effect is that there is a new file
destination
, encoded indestinationEncoding
, the old filesource
is deleted.- Parameters:
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.- Throws:
IOException
-
resolvePath
Return a newFile
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".
- Parameters:
parent
-path
-- Returns:
- Return a new
File
instance for "path".
-
toBytes
Create a byte array with the files content.- Parameters:
file
- The file to read.- Returns:
- Create a byte array with the files content.
- Throws:
IOException
-
toString
Read a file's content at once and return as a string.Use with care!
- Parameters:
file
- The file to read.- Returns:
- The string content of the file.
- Throws:
IOException
-
toString
Read a file's content at once and return as a string in the correct encoding.Use with care!
- Parameters:
file
- The file to read.encoding
- The encoding to use.- Returns:
- The string content of the file.
- Throws:
IOException
-
trimPath
Replaces all characters that are generally not allowed or considered useful in filenames with underscore.- Parameters:
param
- java.lang.String- Returns:
- java.lang.String
-
unlock
-
wait
Wait for a file to arrive.The method waits at most
timeout
milliseconds for a file to arrive. Whendelay
is != 0 the method checks the file's size for changes it reaches a stable size.- Parameters:
file
- The file to wait for.timeout
- The maximum time in milliseconds to wait for first occurence offile
.delay
- The number of milliseconds between two checks against the files size.- Throws:
IOException
-
write
Create a file from the byte content.- Parameters:
file
- The file to write/createbytes
- The data to be written into the file.- Throws:
IOException
-
write
Create a file from the string content.- Parameters:
file
- The file to write/createtext
- The text to be written into the file.- Throws:
IOException
-
write
Create a file from the string content.- Parameters:
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.- Throws:
IOException
-
write
Create a file from the string content.- Parameters:
file
- The file to write/createtext
- The text to be written into the file.- Throws:
IOException
-
write
public static void write(File file, String text, String encoding, boolean append) throws IOException Create a file from the string content / append a string to a file- Parameters:
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.- Throws:
IOException
-