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));
}
}
this.readResources(loader);
}
}
protected abstract void readResources(Map<String, JsonElement> loader);
}
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\$