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 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:
- tru if the candidate path is located inside the parent path, otherwise false
-