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 voidappendFile(File source, File destination) Concatenate the two files given insourceanddestination.static FilecheckDirectory(File dir, boolean create, boolean checkCanRead, boolean checkCanWrite) Utility method for checking the availablity of a directory.static FilecheckDirectory(String path, boolean create, boolean checkCanRead, boolean checkCanWrite) static voidcopyBinaryFile(File source, File destination) Copy the byte content ofsourcetodestination.static voidstatic voidCopy the character content ofsourcetodestination.static voidcopyRecursively(File source, File destination) static FilecopyRecursivelyInto(File source, File destinationParent, String newName) static voidcreateEmptyFile(File file) Create an empty file.static FilecreateTempFile(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 FilecreateTempFile(String filename) Create a file object representing a temporary file in the user's temp dir with the given filename.static voiddeleteAfter(File directory, long millis, boolean recursiveScan) Delete any file indirectorythat is older thanmillismilliseconds.static booleandeleteRecursivly(File file) Deletes a file or directory, if necessary recursivly.static booleandeleteRecursivly(File file, boolean deleteRoot) Deletes a file or directory, if necessary recursivly.static booleanequalsOnSystem(File source, File destination) truewhen the two files represent the same physical file in the file system.static StringgetBaseName(File file) Get the local name of the file in its directory without the extension.static StringgetBaseName(File file, String defaultName) Get the local name of the file in its directory without the extension.static StringgetBaseName(String filename) Get the local name of the file in its directory without the extension.static StringgetBaseName(String filename, String defaultName) Get the local name of the file in its directory without the extension.static Stringstatic StringgetExtension(File file) Get the extension of the file name.static StringgetExtension(String filename) Get the extension of the file name.static StringgetExtension(String filename, String defaultName) Get the extension of the file name.static StringgetFileName(String filename) Get the local name of the file in its directory (with extension).static StringgetFileName(String filename, String defaultName) Get the local name of the file in its directory (with extension).static FilegetParentFile(File file) Try to get a valid parent for file.static StringgetPathRelativeTo(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 StringgetPathRelativeTo(File file, File base, boolean ifAncestor) static booleanisAncestor(File parent, File descendant) static booleanstatic FileTools.Lockstatic voidrenameFile(File source, File destination) static voidrenameFile(File source, String sourceEncoding, File destination, String destinationEncoding) "Rename" a file.static FileresolvePath(File parent, String path) Return a newFileinstance for "path".static byte[]Create a byte array with the files content.static StringRead a file's content at once and return as a string.static StringRead a file's content at once and return as a string in the correct encoding.static StringReplaces all characters that are generally not allowed or considered useful in filenames with underscore.protected static voidunlock(FileTools.Lock lock) static voidWait for a file to arrive.static voidCreate a file from the byte content.static voidCreate a file from the string content.static voidCreate a file from the string content.static voidCreate a file from the string content.static voidCreate 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 insourceanddestination.- 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 ofsourcetodestination.- 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 ofsourcetodestination.- 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 indirectorythat is older thanmillismilliseconds. WhenrecursiveScanistruethe 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
trueif file could be deleted inclusive its components, otherwise false.- Parameters:
file- The file or directory to delete.- Returns:
trueif file could be deleted inclusive its components, otherwise false.
-
deleteRecursivly
Deletes a file or directory, if necessary recursivly.Returns
trueif 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:
trueif file could be deleted inclusive its components, otherwise false.
-
equalsOnSystem
truewhen 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:
truewhen 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 filesourceis 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 newFileinstance 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
Fileinstance 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
timeoutmilliseconds for a file to arrive. Whendelayis != 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
-