Class FileSupport
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intOnly files will be deleted, the directory structure remains untouched.static final intDelete everything including the root directory. -
Method Summary
Modifier and TypeMethodDescriptionstatic FileCopies the specified file fromsourceLocationtotargetLocationand returns a reference to the newly created file or directory.static FileCopies the specifiedsourceLocationtotargetLocationand returns a reference to the newly created file or directory.static booleanDeletes the specified file and returnstrueif the file was deleted.static booleanDeletes the specified file and returnstrueif the file was deleted.static booleanDeletes the content of directorydirand, if specified, the directory itself.static booleandeleteHierarchyIfEmpty(File limit, File start) Delete all directories fromstartup to directorylimitif they are empty.static booleanLikedelete(File)but does not throw any IO exceptions.static booleandeleteQuietly(File f, boolean recurse) Likedelete(File, boolean)but does not throw any IO exceptions.static FileReturns the webapp's temporary work directory.static FilegetTempDirectory(String subdir) Returns a directorysubdirinside the webapp's temporary work directory.static booleanCompare two files by their canonical paths.static booleanCheck ifais a parent ofb.static FileLinks the specified file or directory fromsourceLocationtotargetLocation.static FileLinks the specified file or directory fromsourceLocationtotargetLocation.static voidsetTempDirectory(File tmpDir) Sets the webapp's temporary directory.static booleansupportsLinking(File sourceLocation, File targetLocation) Returnstrueif the operating system as well as the disk layout support creating a hard link fromsrctodest.
-
Field Details
-
DELETE_FILES
public static final int DELETE_FILESOnly files will be deleted, the directory structure remains untouched.- See Also:
-
DELETE_ROOT
public static final int DELETE_ROOTDelete everything including the root directory.- See Also:
-
-
Method Details
-
copy
Copies the specified file fromsourceLocationtotargetLocationand returns a reference to the newly created file or directory.If
targetLocationis an existing directory, then the source file or directory will be copied into this directory, otherwise the source file will be copied to the file identified bytargetLocation.Note that existing files and directories will be overwritten.
Also note that if
targetLocationis a directory than the directory itself, not only its content is copied.- Parameters:
sourceLocation- the source file or directorytargetLocation- the directory to copy the source file or directory to- Returns:
- the created copy
- Throws:
IOException- if copying of the file or directory failed
-
copy
Copies the specifiedsourceLocationtotargetLocationand returns a reference to the newly created file or directory.If
targetLocationis an existing directory, then the source file or directory will be copied into this directory, otherwise the source file will be copied to the file identified bytargetLocation.If
overwriteis set tofalse, this method throws anIOExceptionif the target file already exists.Note that if
targetLocationis a directory than the directory itself, not only its content is copied.- Parameters:
sourceFile- the source file or directorytargetFile- the directory to copy the source file or directory tooverwrite-trueto overwrite existing files- Returns:
- the created copy
- Throws:
IOException- if copying of the file or directory failed
-
link
Links the specified file or directory fromsourceLocationtotargetLocation. IftargetLocationdoes not exist, it will be created, if the target file already exists, anIOExceptionwill be thrown.If this fails (because linking is not supported on the current filesystem, then a copy is made.
- Parameters:
sourceLocation- the source file or directorytargetLocation- the targetLocation- Returns:
- the created link
- Throws:
IOException- if linking of the file or directory failed
-
link
public static File link(File sourceLocation, File targetLocation, boolean overwrite) throws IOException Links the specified file or directory fromsourceLocationtotargetLocation. IftargetLocationdoes not exist, it will be created.If this fails (because linking is not supported on the current filesystem, then a copy is made.
Ifoverwriteis set tofalse, this method throws anIOExceptionif the target file already exists.- Parameters:
sourceLocation- the source file or directorytargetLocation- the targetLocationoverwrite-trueto overwrite existing files- Returns:
- the created link
- Throws:
IOException- if linking of the file or directory failed
-
supportsLinking
Returnstrueif the operating system as well as the disk layout support creating a hard link fromsrctodest. Note that this implementation requires two files rather than directories and will overwrite any existing file that might already be present at the destination.- Parameters:
sourceLocation- the source filetargetLocation- the target file- Returns:
trueif the link was created,falseotherwhise
-
deleteHierarchyIfEmpty
Delete all directories fromstartup to directorylimitif they are empty. Directorylimitis exclusive and will not be deleted.- Returns:
- true if the complete hierarchy has been deleted. false in any other case.
-
isEqual
Compare two files by their canonical paths. -
isParent
Check ifais a parent ofb. This can only be the case ifais a directory and a sub path ofb.isParent(a, a) == true. -
delete
Deletes the specified file and returnstrueif the file was deleted.If
fis a directory, it will only be deleted if it doesn't contain any other files or directories. To do a recursive delete, you may usedelete(File, boolean).- Parameters:
f- the file or directory- Throws:
IOException- See Also:
-
deleteQuietly
Likedelete(File)but does not throw any IO exceptions. In case of an IOException it will only be logged at warning level and the method returns false. -
deleteQuietly
Likedelete(File, boolean)but does not throw any IO exceptions. In case of an IOException it will only be logged at warning level and the method returns false. -
delete
Deletes the specified file and returnstrueif the file was deleted.In the case that
freferences a directory, it will only be deleted if it doesn't contain other files or directories, unlessrecurseis set totrue.- Parameters:
f- the file or directoryrecurse-trueto do a recursive deletes for directories- Throws:
IOException
-
delete
Deletes the content of directorydirand, if specified, the directory itself. Ifdiris a normal file it will always be deleted.- Returns:
- true everthing was deleted, false otherwise
-
setTempDirectory
public static void setTempDirectory(File tmpDir) throws IllegalArgumentException, IllegalStateException Sets the webapp's temporary directory. Make sure that directory exists and has write permissions turned on.- Parameters:
tmpDir- the new temporary directory- Throws:
IllegalArgumentException- if the file object doesn't represent a directoryIllegalStateException- if the directory is write protected
-
getTempDirectory
Returns the webapp's temporary work directory.- Returns:
- the temp directory
-
getTempDirectory
Returns a directorysubdirinside the webapp's temporary work directory.- Parameters:
subdir- name of the subdirectory- Returns:
- the ready to use temp directory
-