Class Resources

java.lang.Object
com.github.ngoanh2n.Resources

public final class Resources extends Object
Find and read Java resources.
  • File file = Resources.getFile("file.json")
  • Path path = Resources.getPath("file.json")
  • String content = Resources.getContent("file.yml")
  • InputStream is = Resources.getInputStream("file.png")
System Property
  • ngoanh2n.findResourceOnClasspath
    Indicate to find the resource file on classpath. Default to true.
    • true: Look for the resources on the classpath
      
                            {project}/out/test/resources/
                            {project}/out/production/resources/
                        
    • false: Look for the resources in root location
      
                            {project}/src/test/resources/
                            {project}/src/main/resources/
                        
Repository:
Since:
2019
  • Method Details

    • getFile

      public static File getFile(@Nonnull String resourceName)
      Get the resource file.
      Parameters:
      resourceName - is the name of resource.
      e.g. com/foo/File.properties
      Returns:
      File of resource if the file exists; RuntimeError otherwise.
    • getPath

      public static Path getPath(@Nonnull String resourceName)
      Get the path of resource.
      Parameters:
      resourceName - is the name of resource.
      e.g. com/foo/File.properties
      Returns:
      Path of resource if the file exists; RuntimeError otherwise.
    • getInputStream

      public static InputStream getInputStream(@Nonnull String resourceName)
      Get the resource file as InputStream.
      Parameters:
      resourceName - is the name of resource.
      e.g. com/foo/File.properties
      Returns:
      InputStream if the file exists; RuntimeError otherwise.
    • getContent

      public static String getContent(@Nonnull String resourceName)
      Get the resource file as String.
      Parameters:
      resourceName - is the name of resource.
      e.g. com/foo/File.properties
      Returns:
      String if the file exists; RuntimeError otherwise.
    • getContent

      public static String getContent(@Nonnull String resourceName, @Nonnull Charset charset)
      Get the resource file as String.
      Parameters:
      resourceName - is the name of resource.
      e.g. com/foo/File.properties
      charset - the charset to use, null means platform default.
      Returns:
      String if the file exists; RuntimeError otherwise.