Package com.axelor.common
Class FileUtils
java.lang.Object
com.axelor.common.FileUtils
This class provides some helper methods to deal with files.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcopyDirectory(File source, File target) Copy the source directory to the target directory.static voidcopyDirectory(Path source, Path target) Copy the source directory to the target directory.static voidCopy the contents of the given source file to the given destination file.static voidstatic voiddeleteDirectory(File directory) Delete the given directory recursively.static voiddeleteDirectory(Path directory) Delete the given directory recursively.static StringgetExtension(String fileName) Gets the extension part of the given fileNamestatic FileGet a file from the given path elements.static FileGet a file from the given path elements.static StringgetFileName(String filePath) Return the name of the filestatic StringgetFileName(Path filePath) Return the name of the filestatic booleanisChildPath(Path parent, Path candidate) Check if the candidate path is located inside the parent pathstatic PathresolveAsChild(Path parent, String child) Safely resolve the given child path against the given parent path, ensuring that the resolved path is located inside the parent path.static PathresolveAsChild(Path parent, Path child) Safely resolve the given child path against the given parent path, ensuring that the resolved path is located inside the parent path.static PathresolveAsSingleChild(Path parent, String child) Safely resolve the given child path against the given parent path, ensuring that the resolved path is located inside the parent path and that the child path is a single segment.static PathresolveAsSingleChild(Path parent, Path child) Safely resolve the given child path against the given parent path, ensuring that the resolved path is located inside the parent path and that the child path is a single segment.static PathresolveSiblingAsChild(Path path, String sibling) Safely resolve the given sibling path against the parent of the given path, ensuring that the resolved path is located inside that parent.static PathresolveSiblingAsChild(Path path, Path sibling) Safely resolve the given sibling path against the parent of the given path, ensuring that the resolved path is located inside that parent.static PathresolveSiblingAsSingleChild(Path path, String sibling) Safely resolve the given sibling path against the parent of the given path, ensuring that the resolved path is located inside that parent and that the sibling path is a single segment.static PathresolveSiblingAsSingleChild(Path path, Path sibling) Safely resolve the given sibling path against the parent of the given path, ensuring that the resolved path is located inside that parent and that the sibling path is a single segment.static StringsafeFileName(String originalFileName) Sanitizes a filename, replacing with dash Removes special characters that are illegal in filenames on certain operating systems Replaces spaces and consecutive underscore with a single dash Trims dot, dash and underscore from beginning and end of filename (with and without the extension part)static StringstripExtension(String fileName) Strip the extension part of the given fileNamestatic voidwrite(File file, InputStream inputStream) Copy the contents of the given InputStream to the given File.static voidwrite(File file, InputStream inputStream, boolean append) Copy the contents of the given InputStream to the given File.static voidwrite(Path path, InputStream inputStream) Copy the contents of the given InputStream to the given Path.static voidwrite(Path path, InputStream inputStream, boolean append) Copy the contents of the given InputStream to the given Path.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
getFile
Get a file from the given path elements.- Parameters:
first- the first path elementmore- the additional path elements- Returns:
- the file
-
getFile
Get a file from the given path elements.- Parameters:
directory- the parent directorynext- next path elementmore- additional path elements- Returns:
- the file
-
copyDirectory
Copy the source directory to the target directory.- Parameters:
source- the source directorytarget- the target directory- Throws:
IOException- if IO error occurs during copying
-
copyDirectory
Copy the source directory to the target directory.- Parameters:
source- the source directorytarget- the target directory- Throws:
IOException- if IO error occurs during copying
-
deleteDirectory
Delete the given directory recursively.- Parameters:
directory- the directory to delete- Throws:
IOException- in case deletion is unsuccessful
-
deleteDirectory
Delete the given directory recursively.- Parameters:
directory- the directory to delete- Throws:
IOException- in case deletion is unsuccessful
-
safeFileName
Sanitizes a filename, replacing with dash- Removes special characters that are illegal in filenames on certain operating systems
- Replaces spaces and consecutive underscore with a single dash
- Trims dot, dash and underscore from beginning and end of filename (with and without the extension part)
- Parameters:
originalFileName- The filename to be sanitized- Returns:
- string The sanitized filename
-
getExtension
Gets the extension part of the given fileNameIt returns the extension of the fileName after the last dot
- Parameters:
fileName- the fileName to retrieve the extension of- Returns:
- the extension of the file or an empty string is not found or null if the given filename is null
-
stripExtension
Strip the extension part of the given fileNameIt returns the name of file before the last dot
- Parameters:
fileName- the fileName to strip the extension- Returns:
- the file name
-
getFileName
Return the name of the file- Parameters:
filePath- the file path- Returns:
- name of the file
-
getFileName
Return the name of the file- Parameters:
filePath- the file path- Returns:
- name of the file
-
copyFile
Copy the contents of the given source file to the given destination file.- Parameters:
source- the source file to copy fromdestination- the destination file to copy to- Throws:
IOException- in case of I/O errors
-
copyPath
- Throws:
IOException
-
write
Copy the contents of the given InputStream to the given File.- Parameters:
file- the file to copy toinputStream- the inputStream to copy from- Throws:
IOException- in case of I/O errors
-
write
Copy the contents of the given InputStream to the given File.- Parameters:
file- the file to copy toinputStream- the inputStream to copy fromappend- write to the end of the file rather than the beginning- Throws:
IOException- in case of I/O errors
-
write
Copy the contents of the given InputStream to the given Path.- Parameters:
path- the path to copy toinputStream- the inputStream to copy from- Throws:
IOException- in case of I/O errors
-
write
Copy the contents of the given InputStream to the given Path.- Parameters:
path- the path to copy toinputStream- the inputStream to copy fromappend- write to the end of the file rather than the beginning- Throws:
IOException- in case of I/O errors
-
isChildPath
Check if the candidate path is located inside the parent path- Parameters:
parent- the parent pathcandidate- the candidate path- Returns:
- true if the candidate path is located inside the parent path, otherwise false
-
resolveAsChild
Safely resolve the given child path against the given parent path, ensuring that the resolved path is located inside the parent path.- Parameters:
parent- the parent pathchild- the child path to resolve against the parent path- Returns:
- the resolved path
- Throws:
IllegalArgumentException- if the resolved path is not located inside the parent path
-
resolveAsChild
Safely resolve the given child path against the given parent path, ensuring that the resolved path is located inside the parent path.- Parameters:
parent- the parent pathchild- the child path to resolve against the parent path- Returns:
- the resolved path
- Throws:
IllegalArgumentException- if the resolved path is not located inside the parent path
-
resolveAsSingleChild
Safely resolve the given child path against the given parent path, ensuring that the resolved path is located inside the parent path and that the child path is a single segment.- Parameters:
parent- the parent pathchild- the child path to resolve against the parent path- Returns:
- the resolved path
- Throws:
IllegalArgumentException- if the resolved path is not located inside the parent path or if the child path is not a single segment
-
resolveAsSingleChild
Safely resolve the given child path against the given parent path, ensuring that the resolved path is located inside the parent path and that the child path is a single segment.- Parameters:
parent- the parent pathchild- the child path to resolve against the parent path- Returns:
- the resolved path
- Throws:
IllegalArgumentException- if the resolved path is not located inside the parent path or if the child path is not a single segment
-
resolveSiblingAsChild
Safely resolve the given sibling path against the parent of the given path, ensuring that the resolved path is located inside that parent.- Parameters:
path- the path whose parent the sibling is resolved againstsibling- the sibling path to resolve against the parent of the given path- Returns:
- the resolved path
- Throws:
IllegalArgumentException- if the resolved path is not located inside the parent of the given path or if the given path has no parent
-
resolveSiblingAsChild
Safely resolve the given sibling path against the parent of the given path, ensuring that the resolved path is located inside that parent.- Parameters:
path- the path whose parent the sibling is resolved againstsibling- the sibling path to resolve against the parent of the given path- Returns:
- the resolved path
- Throws:
IllegalArgumentException- if the resolved path is not located inside the parent of the given path or if the given path has no parent
-
resolveSiblingAsSingleChild
Safely resolve the given sibling path against the parent of the given path, ensuring that the resolved path is located inside that parent and that the sibling path is a single segment.- Parameters:
path- the path whose parent the sibling is resolved againstsibling- the sibling path to resolve against the parent of the given path- Returns:
- the resolved path
- Throws:
IllegalArgumentException- if the resolved path is not located inside the parent of the given path or if the given path has no parent or if the sibling path is not a single segment
-
resolveSiblingAsSingleChild
Safely resolve the given sibling path against the parent of the given path, ensuring that the resolved path is located inside that parent and that the sibling path is a single segment.- Parameters:
path- the path whose parent the sibling is resolved againstsibling- the sibling path to resolve against the parent of the given path- Returns:
- the resolved path
- Throws:
IllegalArgumentException- if the resolved path is not located inside the parent of the given path or if the given path has no parent or if the sibling path is not a single segment
-