Class FileUtils
- Author:
- Yury Molchan
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCopies a file to a new location preserving the file date.static voidDeletes a file.static voidDeletes a path.static voiddeleteQuietly(File file) Deletes a file, never throwing an exception.static voiddeleteQuietly(Path path) static StringgetBaseName(String filename) Gets the base name, minus the full path and extension, from a full filename.static StringgetExtension(String filename) Gets the extension of a filename.static StringGets the name minus the path from a full filename.static StringReturns the path to the system temporary directory.static voidMoves a directory or a file.static StringremoveExtension(String filename) Removes the extension from a filename.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
getTempDirectoryPath
Returns the path to the system temporary directory.- Returns:
- the path to the system temporary directory.
-
copy
Copies a file to a new location preserving the file date.This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.
Note: This method tries to preserve the file's last modified date/times using
File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.- Parameters:
src- an existing file to copy, must not benulldest- the new file, must not benull- Throws:
NullPointerException- if source or destination isnullIOException- if source or destination is invalidIOException- if an IO error occurs during copying
-
move
Moves a directory or a file.When the destination directory or file is on another file system, do a "copy and delete".
- Parameters:
src- the directory or the file to be moveddest- the destination directory or file- Throws:
NullPointerException- if source or destination isnullIOException- if source or destination is invalidIOException- if an IO error occurs moving the file
-
delete
Deletes a file. If file is a directory, delete it and all sub-directories. The difference between File.delete() and this method are:- A directory to be deleted does not have to be empty.
- You get exceptions when a file or directory cannot be deleted.
- Parameters:
file- file or directory to delete, must not benull- Throws:
NullPointerException- if the directory isnullFileNotFoundException- if the file has not been foundIOException- in case deletion is unsuccessful
-
delete
Deletes a path. If provided path is a directory, delete it and all sub-directories.- Parameters:
root- path to file or directory to delete, must not benull- Throws:
NullPointerException- if the directory isnullFileNotFoundException- if the file has not been foundIOException- in case deletion is unsuccessful
-
deleteQuietly
Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories. The difference between File.delete() and this method are:- A directory to be deleted does not have to be empty.
- No exceptions are thrown when a file or directory cannot be deleted.
- Parameters:
file- file or directory to delete, can benull
-
deleteQuietly
-
getName
Gets the name minus the path from a full filename.This method will handle a file in either Unix or Windows format. The text after the last forward or backslash is returned.
a/b/c.txt --> c.txt a.txt --> a.txt a/b/c --> c a/b/c/ --> ""
- Parameters:
filename- the filename to query, null returns null- Returns:
- the name of the file without the path, or an empty string if none exists
-
getBaseName
Gets the base name, minus the full path and extension, from a full filename. This method will handle a file in either Unix or Windows format. The text after the last forward or backslash and before the last dot is returned.a/b/c.txt --> c a.b.txt --> a.b a/b/c --> c a/b/c/ --> ""
- Parameters:
filename- the filename to query, null returns null- Returns:
- the name of the file without the path, or an empty string if none exists
-
getExtension
Gets the extension of a filename.This method returns the textual part of the filename after the last dot. There must be no directory separator after the dot.
a/b/c.txt --> txt a.b.txt --> txt a/b.txt/c --> "" a/b/c --> ""
- Parameters:
filename- the filename to retrieve the extension of.- Returns:
- the extension of the file or an empty string if none exists or
nullif the filename isnull.
-
removeExtension
Removes the extension from a filename.This method returns the textual part of the filename before the last dot. There must be no directory separator after the dot.
foo.txt --> foo a\b\c.jpg --> a\b\c a\b\c --> a\b\c a.b\c --> a.b\c
- Parameters:
filename- the filename to query, null returns null- Returns:
- the filename minus the extension
-