Class FileUtils
java.lang.Object
net.adoptopenjdk.icedteaweb.io.FileUtils
This class contains a few file-related utility functions.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumIndicates whether a file was successfully opened. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidTries to create the ancestor directories of file f.static voidcreateParentDir(File f, String eMsg) Tries to create the ancestor directories of file f.static voiddeleteWithErrMesg(File f, String eMsg) Tries to delete file f.static StringdisplayablePath(String path) Returns a String that is suitable for using in GUI elements for displaying (long) paths to users.static StringdisplayablePath(String path, int visibleChars) Return a String that is suitable for using in GUI elements for displaying paths to users.static FileLockgetFileLock(String path, boolean shared, boolean allowBlock) This will return a lock to the file specified.static byte[]getFileMD5Sum(File file) static StringloadFileAsString(File f, Charset encoding) static Stringstatic voidrecursiveDelete(File file, File base) Recursively delete everything under a directory.static StringsanitizeFileName(String filename) Given an input, return a sanitized form of the input suitable for use as a file/directory namestatic StringsanitizeFileName(String filename, char substitute) static StringsanitizePath(String path) Clean up a string by removing characters that can't appear in a local file name.static StringsanitizePath(String path, char substitute) static voidsaveFileUtf8(String content, File f) Save String into a file in UTF-8 encoding.private static DirectoryCheckResultstestDirectoryPermissions(File file, boolean isDebug) Ensure that the parent directory of the file exists and that we are able to create and access files within this directorystatic FileUtils.OpenFileResulttestFilePermissions(File file, boolean isDebug) Verify that a given file object points to a real, accessible plain file.
-
Field Details
-
LOG
-
MD5
- See Also:
-
WIN_DRIVE_LETTER_COLON_WILDCHAR
- See Also:
-
INVALID_PATH
-
INVALID_NAME
-
SANITIZED_CHAR
private static final char SANITIZED_CHAR- See Also:
-
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
sanitizePath
-
sanitizePath
-
sanitizeFileName
-
sanitizeFileName
-
createParentDir
Tries to create the ancestor directories of file f. Throws an IOException if it can't be created (but not if it was already there).- Parameters:
f- file to provide parent directoryeMsg- - the message to use for the exception. null if the file name is to be used.- Throws:
IOException- if the directory can't be created and doesn't exist.
-
createParentDir
Tries to create the ancestor directories of file f. Throws an IOException if it can't be created (but not if it was already there).- Parameters:
f- file which parent will be created- Throws:
IOException- if the directory can't be created and doesn't exist.
-
deleteWithErrMesg
Tries to delete file f. If the file exists but couldn't be deleted, print an error message to stderr with the file name, or eMsg if eMsg is not null.- Parameters:
f- the file to be deletedeMsg- the message to print on failure (or null to print the the file name).
-
testDirectoryPermissions
Ensure that the parent directory of the file exists and that we are able to create and access files within this directory- Parameters:
file- theFilerepresenting a Java Policy file to testisDebug- output debug information- Returns:
- a
DirectoryCheckResultsobject representing the results of the test
-
testFilePermissions
Verify that a given file object points to a real, accessible plain file.- Parameters:
file- theFileto verifyisDebug- output debug information- Returns:
- an
FileUtils.OpenFileResultrepresenting the accessibility level of the file
-
displayablePath
-
displayablePath
Return a String that is suitable for using in GUI elements for displaying paths to users. If the path is longer than visibleChars, it is truncated in a display-friendly way- Parameters:
path- a path that should be shortedvisibleChars- the maximum number of characters that path should fit into. Also the length of the returned string- Returns:
- a shortened path that contains limited number of chars
-
recursiveDelete
Recursively delete everything under a directory. Works on either files or directories- Parameters:
file- the file object representing what to delete. Can be either a file or a directory.base- the directory under which the file and its subdirectories must be located- Throws:
IOException- on an io exception or if trying to delete something outside the base
-
getFileLock
public static FileLock getFileLock(String path, boolean shared, boolean allowBlock) throws FileNotFoundException This will return a lock to the file specified.- Parameters:
path- File path to file we want to lock.shared- Specify if the lock will be a shared lock.allowBlock- Specify if we should block when we can not get the lock. Getting a shared lock will always block.- Returns:
- FileLock if we were successful in getting a lock, otherwise null.
- Throws:
FileNotFoundException- If the file does not exist.
-
loadFileAsUtf8String
- Throws:
IOException
-
loadFileAsString
- Throws:
IOException
-
saveFileUtf8
Save String into a file in UTF-8 encoding.- Parameters:
content- which will be saved to the filef- file to be saved. No warnings provided- Throws:
IOException- if save fails
-
getFileMD5Sum
- Throws:
NoSuchAlgorithmExceptionIOException
-