Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ResourcePath with list(), listFiles(), listDirectories(), resolve(), stream() #8

Open
calvertdw opened this issue Jan 11, 2018 · 1 comment

Comments

@calvertdw
Copy link
Member

calvertdw commented Jan 11, 2018

Maybe constructor that takes a String with "/" and uses Thread.currentThread().getClassLoader() everywhere.

@calvertdw
Copy link
Member Author

public static List<String> listResourceDirectoryContents(String relativePath)
   {
      try
      {
         return IOUtils.readLines(Thread.currentThread().getContextClassLoader().getResourceAsStream(relativePath), StandardCharsets.UTF_8);
      }
      catch (IOException e)
      {
         throw new RuntimeException(e.getMessage());
      }
   }

   public static InputStream streamFromClassPath(Path path)
   {
      String slashConversion = "";
      for (int i = 0; i < path.getNameCount(); i++)
      {
         slashConversion += path.getName(i).toString();
         if (i < path.getNameCount() - 1)
         {
            slashConversion += "/";
         }
      }

      return Thread.currentThread().getContextClassLoader().getResourceAsStream(slashConversion);
   }

   public static File fileFromClassPath(Path path)
   {
      String slashConversion = "";
      for (int i = 0; i < path.getNameCount(); i++)
      {
         slashConversion += path.getName(i).toString();
         if (i < path.getNameCount() - 1)
         {
            slashConversion += "/";
         }
      }

      URL resourceUrl = Thread.currentThread().getContextClassLoader().getResource(slashConversion);
      String resourceUrlPath = resourceUrl.getPath();
      if (SystemUtils.IS_OS_WINDOWS)
         resourceUrlPath = resourceUrlPath.substring(1, resourceUrlPath.length());

      return Paths.get(resourceUrlPath).toFile();
   }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant