Class StringUtils

java.lang.Object
org.ujorm.tools.common.StringUtils

public class StringUtils extends Object
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 Details

  • Constructor Details

    • StringUtils

      public StringUtils()
      With a charset UTF-8
    • StringUtils

      public StringUtils(@NotNull @NotNull Charset charset)
    • StringUtils

      public StringUtils(@NotNull @NotNull Charset charset, @NotNull @NotNull Class<?> classOfLoader)
  • Method Details

    • readBody

      @NotNull public @NotNull String readBody(@NotNull @NotNull String... resource)
      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

      @NotNull public @NotNull String readBody(@NotNull @NotNull InputStream is)
      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

      @NotNull public @NotNull Stream<String> readRows(@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
    • 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

      @NotNull public @NotNull Stream<String> readRows(@NotNull @NotNull Path file) throws IOException
      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

      @NotNull public @NotNull Stream<String> readRows(@NotNull @NotNull File file) throws IOException
      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

      @NotNull public static @NotNull String read(@NotNull @NotNull InputStream is)
      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

      @NotNull public static @NotNull Stream<String> readLines(@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.