Package com.axelor.common
Class FileUtils
- java.lang.Object
-
- com.axelor.common.FileUtils
-
public final class FileUtils extends Object
This class provides some helper methods to deal with files.
-
-
Constructor Summary
Constructors Constructor Description FileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
copyDirectory(File source, File target)
Copy the source directory to the target directory.static void
copyDirectory(Path source, Path target)
Copy the source directory to the target directory.static void
deleteDirectory(File directory)
Delete the given directory recursively.static void
deleteDirectory(Path directory)
Delete the given directory recursively.static String
getExtension(String fileName)
Gets the extension part of the given fileNamestatic File
getFile(File directory, String next, String... more)
Get a file from the given path elements.static File
getFile(String first, String... more)
Get a file from the given path elements.static String
safeFileName(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 String
stripExtension(String fileName)
Strip the extension part of the given fileName
-
-
-
Method Detail
-
getFile
public static File getFile(String first, String... more)
Get a file from the given path elements.- Parameters:
first
- the first path elementmore
- the additional path elements- Returns:
- the file
-
getFile
public static File getFile(File directory, String next, String... more)
Get a file from the given path elements.- Parameters:
directory
- the parent directorynext
- next path elementmore
- additional path elements- Returns:
- the file
-
copyDirectory
public static void copyDirectory(File source, File target) throws IOException
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
public static void copyDirectory(Path source, Path target) throws IOException
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
public static void deleteDirectory(File directory) throws IOException
Delete the given directory recursively.- Parameters:
directory
- the directory to delete- Throws:
IOException
- in case deletion is unsuccessful
-
deleteDirectory
public static void deleteDirectory(Path directory) throws IOException
Delete the given directory recursively.- Parameters:
directory
- the directory to delete- Throws:
IOException
- in case deletion is unsuccessful
-
safeFileName
public static String safeFileName(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)
- Parameters:
originalFileName
- The filename to be sanitized- Returns:
- string The sanitized filename
-
getExtension
public static String getExtension(String fileName)
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
-
-