Skip to content
Immortius edited this page Nov 30, 2019 · 1 revision

Module Resources

Modules provide access to their resources through a ModuleFileSource - this provides an abstraction around sources of files to allow a common way to access resources regardless of whether they are on the filesystem, classpath or in an archive (Note: NIO could be used to do this too but requires a more recent versions of Java or Android - so this is used for broader compatibility).

Accessing Files

Files can be retrieved using ModuleFileSource's

  • getFile:
moduleFileSource.getFile("folder", "subfolder", "filename");
  • getFiles to iterate all files:
for (FileReference file : moduleFileSource.getFiles()) {
  // ...
}
  • getFilesInPath, which can be optionally recursive:
for (FileReference file : moduleFileSource.getFilesInPath(true, "folder", "subfolder") {
  // ...
}

A listing of subpaths can be obtained using the getSubpaths method.

Finally, if running on a newer version of Java or Android, the getRootPaths method can be used to obtain java.nio.file.Path objects for any directories that are part of the module. This can be used establish a watch service over those directories.

ModuleEnvironment Resources

ModuleEnvironment also provides access to a ModuleFileSource - this combines all the files of all modules. If there are multiple modules providing the same resource at the same location, the file provided by the first module in dependency order can be accessed in this way. So generally it is recommended to access files through individual modules rather than the environment.