java.lang.Object
net.luis.utils.io.FileUtils
Utility class for file operations.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation.
This is a static helper class. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCreates the given file and all parent directories.static voidcreateIfNotExists(@NotNull Path file) Creates the given file and all parent directories if they do not exist.static @NotNull PathcreateSessionDirectory(@Nullable String prefix) Creates a temporary directory for the current session.
The directory and all its content will be deleted when the JVM shuts down.static voiddeleteRecursively(@NotNull Path path) Deletes the given file or directory recursively.static @NotNull StringgetExtension(@Nullable String file) Gets the extension of the given file.
The file can be a path or a file name.static @NotNull StringGets the name of the given file.
The file can be a path or a file name.static @NotNull StringgetRelativePath(@Nullable String file) Gets the relative path of the given file.
The file can be a path or a file name.static @NotNull StringreadString(@NotNull Reader reader) Reads the content of the given reader as a string.static @NotNull StringreadString(@NotNull InputProvider provider) Reads the content of the given input provider as a string.static @NotNull StringreadString(@NotNull InputProvider provider, @NotNull Charset charset) Reads the content of the given input provider as a string using the given charset.Splits the given file into a pair of the folder and the file name.
The folder is the part before the last slash, the name is the part after the last slash.
-
Constructor Details
-
FileUtils
private FileUtils()Private constructor to prevent instantiation.
This is a static helper class.
-
-
Method Details
-
split
Splits the given file into a pair of the folder and the file name.
The folder is the part before the last slash, the name is the part after the last slash.
Examples:
split(null) -> ("", "") split("") -> ("", "") split(".") -> (".", "") split("./") -> ("./", "") split("foo") -> ("foo", "") split("/foo/") -> ("/foo"/, "") split("/foo.json") -> ("/", "foo.json") split("/bar/foo.json") -> ("/bar"/, "foo.json")- Parameters:
file- The file to split as a string- Returns:
- The pair of the folder and the file name
-
getName
Gets the name of the given file.
The file can be a path or a file name.
Examples:
getName(null) -> "" getName("") -> "" getName("/") -> "" getName("/foo.json") -> "foo" getName("/bar/foo.json") -> "foo"- Parameters:
file- The file to get the name of- Returns:
- The name of the file or an empty string if the file has no name
-
getExtension
Gets the extension of the given file.
The file can be a path or a file name.
Examples:
getExtension(null) -> "" getExtension("") -> "" getExtension("/") -> "" getExtension("/foo.json") -> "json" getExtension("/bar/foo.json") -> "json"- Parameters:
file- The file to get the extension of- Returns:
- The extension of the file or an empty string if the file has no extension
-
getRelativePath
Gets the relative path of the given file.
The file can be a path or a file name.Examples:
getRelativePath(null) -> "./" getRelativePath("") -> "./" getRelativePath("/") -> "./" getRelativePath("foo") -> "./foo/" getRelativePath("./foo/") -> "./foo/" getRelativePath("/foo.json") -> "./foo.json" getRelativePath("/bar/foo.json") -> "./bar/foo.json"- Parameters:
file- The file to get the relative path of- Returns:
- The relative path of the file
-
create
Creates the given file and all parent directories.- Parameters:
file- The file to create- Throws:
NullPointerException- If the file is nullFileAlreadyExistsException- If the file already existsIOException- If an I/O error occurs
-
createIfNotExists
Creates the given file and all parent directories if they do not exist.- Parameters:
file- The file to create- Throws:
NullPointerException- If the file is nullIOException- If an I/O error occurs- See Also:
-
createSessionDirectory
@Blocking @NotNull public static @NotNull Path createSessionDirectory(@Nullable @Nullable String prefix) throws IOException Creates a temporary directory for the current session.
The directory and all its content will be deleted when the JVM shuts down.- Parameters:
prefix- The prefix of the directory name- Returns:
- The path of the created directory
- Throws:
IOException- If an I/O error occurs- See Also:
-
deleteRecursively
Deletes the given file or directory recursively.- Parameters:
path- The path to delete- Throws:
NullPointerException- If the path is nullIOException- If an I/O error occurs
-
readString
@Blocking @NotNull public static @NotNull String readString(@NotNull @NotNull InputProvider provider) throws IOException Reads the content of the given input provider as a string.- Parameters:
provider- The input provider to read from- Returns:
- The content of the input provider
- Throws:
NullPointerException- If the provider is nullIOException- If an I/O error occurs
-
readString
@Blocking @NotNull public static @NotNull String readString(@NotNull @NotNull InputProvider provider, @NotNull @NotNull Charset charset) throws IOException Reads the content of the given input provider as a string using the given charset.- Parameters:
provider- The input provider to read fromcharset- The charset to use- Returns:
- The content of the input provider
- Throws:
NullPointerException- If the provider or charset is nullIOException- If an I/O error occurs
-
readString
@Blocking @NotNull public static @NotNull String readString(@NotNull @NotNull Reader reader) throws IOException Reads the content of the given reader as a string.- Parameters:
reader- The reader to read from- Returns:
- The content of the reader
- Throws:
NullPointerException- If the reader is nullIOException- If an I/O error occurs
-