public class Files extends Object
Modifier and Type | Method and Description |
---|---|
static void |
delete(File file)
If the given file is not null and exists, tries to delete that file and logs when a file cannot be deleted.
|
static void |
delete(Path directory)
Deletes the given directory structure including all sub directories.
|
static String |
getBasepath(String path)
Returns the basepath of the given path to a file.
|
static String |
getFileExtension(String path)
Returns the file extension of the given path or filename.
|
static String |
getFilenameAndExtension(String path)
Returns the filename with the file extension of the given path.
|
static String |
getFilenameWithoutExtension(String path)
Returns the filename without its file extension.
|
static void |
removeChildren(Path directory)
Removes all children (files and directories) of the given directory.
|
static Optional<String> |
toSaneFileName(String input)
Converts the given string to a valid and sane filename.
|
@Nonnull public static Optional<String> toSaneFileName(@Nullable String input)
The result will only consist of letters, digits '-' and '_'. Everything else will be replaced by a '_'. German umlauts like 'ä', 'ö', 'ü' are replaced the appropriate ASCII sequences.
input
- the filename to fix@Nullable public static String getFileExtension(@Nullable String path)
A path could be /dir/foo/test.txt, a filename would be test.txt.
path
- the path to parse@Nullable public static String getBasepath(@Nullable String path)
The basepath is everything but the filename of the given path. So /foo/bar/test.txt will yield /foo/bar as as path. text.txt will yield null.
Note that both / and \ are accepted as path separators and are preserved in the output.
path
- the path to a file to parse@Nullable public static String getFilenameAndExtension(@Nullable String path)
The filename is everything after the last separator (/ or \). If the given path does not contain a separator, the whole path will be returned.
path
- the path to a file to parse@Nullable public static String getFilenameWithoutExtension(@Nullable String path)
The file extension is everything after the last . in the filename.
path
- the path to a file to parsepublic static void delete(@Nullable File file)
file
- the file to deletepublic static void delete(Path directory) throws IOException
directory
- the directory to delete (along with all sub directories)IOException
- in case of an io error while deleting the files and directoriespublic static void removeChildren(Path directory) throws IOException
directory
- the containing the children to delete (along with all sub directories)IOException
- in case of an io error while deleting the files and directoriesCopyright © 2018. All rights reserved.