So I'm making a utility class to find resources and read them as JSON objects using the GSON library, the code below is working good but I don't really know how good it is, so I would like to get opinions about it and maybe some things I can improve and make it more efficient?

public abstract class ResourceManager {

    private final String resourcesDirectoryName;

    public ResourceManager(String resourcesDirectoryName) {
        this.resourcesDirectoryName = resourcesDirectoryName;

    public void findAndReadResources() {
        Map<String, JsonElement> loader = new HashMap<>();

        InputStream resourcesDirectoryStream = this.getClass().getClassLoader().getResourceAsStream(resourcesDirectoryName);
        if(resourcesDirectoryStream != null) {
            Scanner resourcesDirectoryReader = new Scanner(resourcesDirectoryStream);
            while(resourcesDirectoryReader.hasNextLine()) {
                String resourceFileName = resourcesDirectoryReader.nextLine();
                InputStream resourceFileStream = JTasks.class.getClassLoader().getResourceAsStream(
                        resourcesDirectoryName + "\\" + resourceFileName);
                if(resourceFileStream != null) {
                    Reader resourceFileReader = new InputStreamReader(resourceFileStream);
                    loader.put(resourceFileName, JsonParser.parseReader(resourceFileReader));

    protected abstract void readResources(Map<String, JsonElement> loader);
  • 1
    \$\begingroup\$ Could you please provide a class that inherits from this class as well? \$\endgroup\$
    – pacmaninbw
    Oct 26, 2021 at 15:53
  • \$\begingroup\$ ``` public class JsonsResourceManager extends ResourceManager { public JsonsResourceManager() { super("jsons"); } @Override protected void readResources(Map<String, JsonElement> loader) { loader.forEach((string, jsonElement) -> { System.out.println(string); System.out.println(jsonElement); }); } } ``` Don't really know how it'll help you, but here's an example. \$\endgroup\$
    – Boofi
    Oct 26, 2021 at 16:10
  • \$\begingroup\$ I meant that you should add the code to the question. When we are asked to make code more efficient, the more code there is to review the better we can help with efficiency. \$\endgroup\$
    – pacmaninbw
    Oct 26, 2021 at 19:50
  • \$\begingroup\$ The names like resourcesDirectoryName imply that the resources are plain files in the filesystem. On the other hand, getResourceAsStream() often accesses e.g. Jar file contents. What is your use case? \$\endgroup\$ Oct 28, 2021 at 9:02


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.