Package org.apache.ivy.util
Class FileUtil
java.lang.Object
org.apache.ivy.util.FileUtil
Utility class used to deal with file related operations, like copy, full reading, symlink, ...
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
static boolean
copy
(File src, File dest, CopyProgressListener l) This is the same as callingcopy(File, File, CopyProgressListener, boolean)
withoverwrite
param astrue
static boolean
copy
(File src, File dest, CopyProgressListener l, boolean overwrite) static void
copy
(File src, URL dest, CopyProgressListener listener, TimeoutConstraint timeoutConstraint) static void
copy
(InputStream src, File dest, CopyProgressListener l) static void
copy
(InputStream src, OutputStream dest, CopyProgressListener l) static void
copy
(InputStream src, OutputStream dest, CopyProgressListener l, boolean autoClose) static void
copy
(URL src, File dest, CopyProgressListener listener, TimeoutConstraint timeoutConstraint) static boolean
deepCopy
(File src, File dest, CopyProgressListener l, boolean overwrite) static boolean
forceDelete
(File file) Recursively delete filestatic long
getFileLength
(File file) Get the length of the file, or the sum of the children lengths if it is a directorygetPathFiles
(File root, File file) Returns a list of Files composed of all directories being parent of file and child of root + file and root themselves.static boolean
isLeadingPath
(File leading, File path) Learn whether one path "leads" another.static boolean
isLeadingPath
(File leading, File path, boolean resolveSymlinks) Learn whether one path "leads" another.static Collection<File>
listAll
(File dir, Collection<String> ignore) static File
"Normalize" the given absolute path.static boolean
prepareCopy
(File src, File dest, boolean overwrite) static String
Reads the whole BufferedReader line by line, using \n as line separator for each line.static String
readEntirely
(File f) Reads the entire content of the file and returns it as a String.static String
Reads the entire content of theInputStream
and returns it as a String.static File
resolveFile
(File file, String filename) static boolean
Creates a symbolic link atlink
whose target will be thetarget
.static InputStream
unwrapPack200
(InputStream packed)
-
Method Details
-
symlink
Creates a symbolic link atlink
whose target will be thetarget
. Depending on the underlying filesystem, this method may not always be able to create a symbolic link, in which case this method returnsfalse
.- Parameters:
target
- TheFile
which will be the target of the symlink being createdlink
- The path to the symlink that needs to be createdoverwrite
-true
if any existing file atlink
has to be overwritten. False otherwise- Returns:
- Returns true if the symlink was successfully created. Returns false if the symlink could not be created
- Throws:
IOException
- ifFiles.createSymbolicLink(java.nio.file.Path, java.nio.file.Path, java.nio.file.attribute.FileAttribute<?>...)
fails
-
copy
This is the same as callingcopy(File, File, CopyProgressListener, boolean)
withoverwrite
param astrue
- Parameters:
src
- The source to copydest
- The destinationl
- ACopyProgressListener
. Can be null- Returns:
- Returns true if the file was copied. Else returns false
- Throws:
IOException
- If any exception occurs during the copy operation
-
prepareCopy
- Throws:
IOException
-
copy
public static boolean copy(File src, File dest, CopyProgressListener l, boolean overwrite) throws IOException - Throws:
IOException
-
deepCopy
public static boolean deepCopy(File src, File dest, CopyProgressListener l, boolean overwrite) throws IOException - Throws:
IOException
-
copy
public static void copy(URL src, File dest, CopyProgressListener listener, TimeoutConstraint timeoutConstraint) throws IOException - Throws:
IOException
-
copy
public static void copy(File src, URL dest, CopyProgressListener listener, TimeoutConstraint timeoutConstraint) throws IOException - Throws:
IOException
-
copy
- Throws:
IOException
-
copy
public static void copy(InputStream src, OutputStream dest, CopyProgressListener l) throws IOException - Throws:
IOException
-
copy
public static void copy(InputStream src, OutputStream dest, CopyProgressListener l, boolean autoClose) throws IOException - Throws:
IOException
-
readEntirely
Reads the whole BufferedReader line by line, using \n as line separator for each line.Note that this method will add a final \n to the last line even though there is no new line character at the end of last line in the original reader.
The BufferedReader is closed when this method returns.
- Parameters:
in
- theBufferedReader
to read from- Returns:
- a String with the whole content read from the
BufferedReader
- Throws:
IOException
- if an IO problems occur during reading
-
readEntirely
Reads the entire content of the file and returns it as a String.- Parameters:
f
- the file to read from- Returns:
- a String with the file content
- Throws:
IOException
- if an IO problems occurs during reading
-
readEntirely
Reads the entire content of theInputStream
and returns it as a String.The input stream is closed when this method returns.
- Parameters:
is
- theInputStream
to read from- Returns:
- a String with the input stream content
- Throws:
IOException
- if an IO problems occurs during reading
-
concat
-
forceDelete
Recursively delete file- Parameters:
file
- the file to delete- Returns:
- true if the deletion completed successfully (ie if the file does not exist on the filesystem after this call), false if a deletion was not performed successfully.
-
getPathFiles
Returns a list of Files composed of all directories being parent of file and child of root + file and root themselves. Example:getPathFiles(new File("test"), new File("test/dir1/dir2/file.txt")) => {new File("test/dir1"), new File("test/dir1/dir2"), new File("test/dir1/dir2/file.txt") }
Note that if root is not an ancestor of file, or if root is null, all directories from the file system root will be returned.- Parameters:
root
- Filefile
- File- Returns:
- List<File>
-
listAll
- Parameters:
dir
- The directory from which all files, including files in subdirectory) are extracted.ignore
- a Collection of filenames which must be excluded from listing- Returns:
- a collection containing all the files of the given directory and it's subdirectories, recursively.
-
resolveFile
-
normalize
"Normalize" the given absolute path.This includes:
- Uppercase the drive letter if there is one.
- Remove redundant slashes after the drive spec.
- Resolve all ./, .\, ../ and ..\ sequences.
- DOS style paths that start with a drive letter will have \ as the separator.
File.getCanonicalPath()
this method specifically does not resolve symbolic links.- Parameters:
path
- the path to be normalized.- Returns:
- the normalized version of the path.
- Throws:
NullPointerException
- if path is null.
-
isLeadingPath
Learn whether one path "leads" another.This method uses
normalize(java.lang.String)
under the covers and does not resolve symbolic links.If either path tries to go beyond the file system root (i.e. it contains more ".." segments than can be travelled up) the method will return false.
- Parameters:
leading
- The leading path, must not be null, must be absolute.path
- The path to check, must not be null, must be absolute.- Returns:
- true if path starts with leading; false otherwise.
- Since:
- Ant 1.7
-
isLeadingPath
public static boolean isLeadingPath(File leading, File path, boolean resolveSymlinks) throws IOException Learn whether one path "leads" another.- Parameters:
leading
- The leading path, must not be null, must be absolute.path
- The path to check, must not be null, must be absolute.resolveSymlinks
- whether symbolic links shall be resolved prior to comparing the paths.- Returns:
- true if path starts with leading; false otherwise.
- Throws:
IOException
- if resolveSymlinks is true and invoking getCanonicaPath on either argument throws an exception- Since:
- Ant 1.9.13
-
getFileLength
Get the length of the file, or the sum of the children lengths if it is a directory- Parameters:
file
- File- Returns:
- long
-
unwrapPack200
- Throws:
IOException
-