Package tools.common
Class StringUtils
java.lang.Object
tools.common.StringUtils
Methods to reading an text resource to the
String.
Usage
try (Stream<String> stream = StringUtils.readLines(StringUtils.class, "text", "dummy.txt")) {
String[] result = stream.toArray(String[]::new);
assertEquals("abc", result[0]);
}
- Author:
- Pavel Ponec
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionWith a charset UTF-8StringUtils(@NotNull Charset charset) StringUtils(@NotNull Charset charset, @NotNull Class<?> classOfLoader) -
Method Summary
Modifier and TypeMethodDescriptionprotected StringbuildResource(@Nullable Class<?> basePackage, @NotNull String... resourcePath) Build a resourcestatic @NotNull Stringread(@NotNull InputStream is) Read a content of the resource encoded by UTF-8.static @NotNull StringRead a content of the resource encoded by UTF-8.@NotNull StringreadBody(@NotNull InputStream is) Read a content of the resource encoded by UTF-8.@NotNull StringRead a content of the resource encoded by UTF-8.@NotNull StringRead a content of the resource encoded by UTF-8.Read a content of the resource encoded by UTF-8.Read a closeable line stream of the URL.Read a content of the resource encoded by UTF-8.Read a closeable line stream of the File.readRows(@NotNull InputStream is) Read a closeable line stream of the URL.Read a closeable line stream of the URL.Read a closeable line stream of the File.Read a content of the resource encoded by UTF-8.
-
Field Details
-
SEPARATOR
File separator (one character is required)- See Also:
-
-
Constructor Details
-
StringUtils
public StringUtils()With a charset UTF-8 -
StringUtils
-
StringUtils
-
-
Method Details
-
readBody
Read a content of the resource encoded by UTF-8. A line separator can be modified in the result. -
readBody
@NotNull public @NotNull String readBody(@NotNull @NotNull Class<?> basePackage, @NotNull @NotNull String... resourcePath) Read a content of the resource encoded by UTF-8. A line separator can be modified in the result. -
readBody
Read a content of the resource encoded by UTF-8. A line separator can be modified in the result. -
readRows
@NotNull public @NotNull Stream<String> readRows(@Nullable @Nullable Class<?> basePackage, @NotNull @NotNull String... resourcePath) Read a content of the resource encoded by UTF-8. A line separator can be modified in the result.- Returns:
- The result must be closed.
-
readRows
Read a closeable line stream of the URL. A line separator can be modified in the result.- Returns:
- The result must be closed.
- Throws:
IOException
-
readRows
@NotNull public @NotNull Stream<String> readRows(@NotNull @NotNull InputStream is) throws IOException Read a closeable line stream of the URL. A line separator can be modified in the result- Returns:
- The result must be closed.
- Throws:
IOException
-
readRows
Read a closeable line stream of the File.
In Java 17, the following code can be used with a similar result:
Files.lines(Path.of("File.txt"), StandardCharsets.UTF_8)- Throws:
IOException
-
readRows
Read a closeable line stream of the File.- Throws:
IOException
-
buildResource
protected String buildResource(@Nullable @Nullable Class<?> basePackage, @NotNull @NotNull String... resourcePath) Build a resource -
read
@NotNull public static @NotNull String read(@NotNull @NotNull Class<?> basePackage, @NotNull @NotNull String... resourcePath) Read a content of the resource encoded by UTF-8. A line separator can be modified in the result. -
read
Read a content of the resource encoded by UTF-8. A line separator can be modified in the result. -
readLines
@NotNull public static @NotNull Stream<String> readLines(@NotNull @NotNull URL url) throws IOException Read a closeable line stream of the URL. A line separator can be modified in the result.- Returns:
- The result must be closed.
- Throws:
IOException
-
readLines
@NotNull public static @NotNull Stream<String> readLines(@Nullable @Nullable Class<?> basePackage, @NotNull @NotNull String... resourcePath) Read a content of the resource encoded by UTF-8. A line separator can be modified in the result.- Returns:
- The result must be closed.
-
readLines
Read a content of the resource encoded by UTF-8. A line separator can be modified in the result.- Returns:
- The result must be closed.
-