@Immutable public class IoUtil extends Object
| Modifier and Type | Field and Description |
|---|---|
private static org.slf4j.Logger |
LOGGER |
| Modifier | Constructor and Description |
|---|---|
private |
IoUtil() |
| Modifier and Type | Method and Description |
|---|---|
static File |
createDirectory(Path path)
Create a directory at the given absolute or relative path.
|
static File |
createDirectory(Path path,
boolean removeExistingContent)
Create a directory at the given absolute or relative path, removing any existing content beforehand.
|
static File |
createFile(Path path)
Create a file at the given absolute or relative path.
|
static void |
delete(File... filesOrFolder)
A method that will delete multiple file and/or folders.
|
static void |
delete(File fileOrFolder)
A method that will delete a file or folder.
|
static void |
delete(Path path)
A method that will recursively delete a file or folder.
|
static void |
delete(String path)
A method that will delete a file or folder only if it is within the 'target' directory (for safety).
|
static int |
getAvailablePort()
Find a port that is available.
|
static InputStream |
getResourceAsStream(String resourcePath,
ClassLoader classLoader,
Class<?> clazz,
String resourceDesc,
Consumer<String> logger)
Get the
input stream to the resource given by the supplied path. |
static Properties |
loadProperties(Class<?> clazz,
String classpathResource)
Atomically load the properties file at the given location within the designated class' class loader.
|
static Properties |
loadProperties(ClassLoader classLoader,
String classpathResource)
Atomically load the properties file at the given location within the designated class loader.
|
static Properties |
loadProperties(Supplier<ClassLoader> classLoader,
String classpathResource)
Atomically load the properties file at the given location within the designated class loader.
|
private static void |
logMessage(InputStream stream,
Consumer<String> logger,
String resourceDesc,
String msg) |
static String |
read(File file)
Read and return the entire contents of the supplied
File. |
static String |
read(InputStream stream)
Read and return the entire contents of the supplied
InputStream. |
static String |
read(InputStream stream,
String charset)
Read and return the entire contents of the supplied
InputStream. |
static String |
read(Reader reader)
Read and return the entire contents of the supplied
Reader. |
static byte[] |
readBytes(File file)
Read and return the entire contents of the supplied
file. |
static byte[] |
readBytes(InputStream stream)
Read and return the entire contents of the supplied
stream. |
static void |
readLines(InputStream stream,
Consumer<String> lineProcessor)
Read the lines from the supplied stream.
|
static void |
readLines(InputStream stream,
Consumer<String> lineProcessor,
Charset charset)
Read the lines from the supplied stream.
|
static void |
readLines(Path path,
Consumer<String> lineProcessor)
Read the lines from the supplied stream.
|
static void |
readLines(String resourcePath,
ClassLoader classLoader,
Class<?> clazz,
Consumer<String> lineProcessor)
Read the lines from the content of the resource file at the given path on the classpath.
|
public static byte[] readBytes(InputStream stream) throws IOException
stream. This method always closes the stream when
finished reading.stream - the stream to the contents; may be nullIOException - if there is an error reading the contentpublic static byte[] readBytes(File file) throws IOException
file.file - the file containing the contents; may be nullIOException - if there is an error reading the contentpublic static void readLines(String resourcePath, ClassLoader classLoader, Class<?> clazz, Consumer<String> lineProcessor) throws IOException
resourcePath - the logical path to the classpath, file, or URL resourceclassLoader - the classloader that should be used to load the resource as a stream; may be nullclazz - the class that should be used to load the resource as a stream; may be nulllineProcessor - the function that this method calls for each line read from the supplied stream; may not be nullIOException - if an I/O error occurspublic static void readLines(InputStream stream, Consumer<String> lineProcessor) throws IOException
stream - the stream with the contents to be read; may not be nulllineProcessor - the function that this method calls for each line read from the supplied stream; may not be nullIOException - if an I/O error occurspublic static void readLines(InputStream stream, Consumer<String> lineProcessor, Charset charset) throws IOException
stream - the stream with the contents to be read; may not be nulllineProcessor - the function that this method calls for each line read from the supplied stream; may not be nullcharset - the character set used to interpret the stream contentIOException - if an I/O error occurspublic static void readLines(Path path, Consumer<String> lineProcessor) throws IOException
path - path to the file with the contents to be read; may not be nulllineProcessor - the function that this method calls for each line read from the supplied stream; may not be nullIOException - if an I/O error occurspublic static String read(Reader reader) throws IOException
Reader. This method always closes the reader when finished
reading.reader - the reader of the contents; may be nullIOException - if there is an error reading the contentpublic static String read(InputStream stream) throws IOException
InputStream. This method always closes the stream when finished
reading.stream - the streamed contents; may be nullIOException - if there is an error reading the contentpublic static String read(InputStream stream, String charset) throws IOException
InputStream. This method always closes the stream when finished
reading.stream - the streamed contents; may be nullcharset - character set of the stream data; may not be nullIOException - if there is an error reading the contentpublic static String read(File file) throws IOException
File.file - the file containing the information to be read; may be nullIOException - if there is an error reading the contentpublic static InputStream getResourceAsStream(String resourcePath, ClassLoader classLoader, Class<?> clazz, String resourceDesc, Consumer<String> logger)
input stream to the resource given by the supplied path. This method performs these operations
in order, returning as soon as a file is found:
classloader is supplied, use it to load the file on the classpath at the given path; otherwiseclazz is supplied, use it to load the file on its classpath at the given path; otherwiseresourcePath - the logical path to the classpath, file, or URL resourceclassLoader - the classloader that should be used to load the resource as a stream; may be nullclazz - the class that should be used to load the resource as a stream; may be nullresourceDesc - the description of the resource to be used in messages sent to logger; may be nulllogger - a function that is to be called with log messages describing what is being tried; may be nullIllegalArgumentException - if the resource path is null or emptypublic static File createDirectory(Path path)
path - the relative or absolute path of the directory; may not be nullpublic static File createFile(Path path)
path - the relative or absolute path of the file to create; may not be nullpublic static File createDirectory(Path path, boolean removeExistingContent) throws IOException
path - the relative or absolute path of the directory to recreate; may not be nullremoveExistingContent - true if any existing content should be removedIOException - if there is a problem deleting the files at this pathpublic static void delete(String path) throws IOException
path - the path to the file or folder in the target directoryIOException - if there is a problem deleting the file at the given pathpublic static void delete(File fileOrFolder) throws IOException
fileOrFolder - the file or folder to be deletedIOException - if there is a problem deleting the file at the given pathpublic static void delete(File... filesOrFolder) throws IOException
filesOrFolder - the files and folders to be deletedIOException - if there is a problem deleting the file at the given pathpublic static void delete(Path path) throws IOException
path - the path to the file or folder in the target directoryIOException - if there is a problem deleting the file at the given pathpublic static int getAvailablePort()
ServerSocket and obtains the port on which the socket is
listening, and then shuts down the socket so the port becomes available.IllegalStateException - if it cannot find an available portprivate static void logMessage(InputStream stream, Consumer<String> logger, String resourceDesc, String msg)
public static Properties loadProperties(Supplier<ClassLoader> classLoader, String classpathResource)
classLoader - the supplier for the class loader; may not be null or return nullclasspathResource - the path to the resource file; may not be nullIllegalStateException - if the file could not be found or readpublic static Properties loadProperties(ClassLoader classLoader, String classpathResource)
classLoader - the class loader; may not be nullclasspathResource - the path to the resource file; may not be nullIllegalStateException - if the file could not be found or readpublic static Properties loadProperties(Class<?> clazz, String classpathResource)
clazz - the class whose class loader is to be used; may not be nullclasspathResource - the path to the resource file; may not be nullIllegalStateException - if the file could not be found or readCopyright © 2019 JBoss by Red Hat. All rights reserved.