0

Below code, i am manually write "a.json", "b.json". But i want to get all file names in resources directory and is it possible?

public static void loadFiles() throws IOException {
    ClassLoader classLoader = ClassLoader.getSystemClassLoader();

    List<String> fileNames = List.of("a.json", "b.json"); // Bad
    // List<String> fileNames = getFileNamesInResources(); // Good


    for (int i = 0; i < fileNames.size(); i++) {
        String fileName = fileNames.get(i);
        InputStream is = classLoader.getResourceAsStream(fileName);
        String content = new String(is.readAllBytes(), StandardCharsets.UTF_8);
        System.out.println(content);
        is.close();
    }
}
motgochim
  • 3
  • 2
  • 1
    Generally no; what I’ve done in past is have a build step which generates a “manifest” file (containing the names of the files) during the build phase, which is embedded with other resources. At runtime, I’d read the manifest file and then be able to reference each embedded resource dynamically – MadProgrammer Dec 24 '22 at 20:16
  • The answers to [this Q&A](https://stackoverflow.com/q/73309923/6395627) might provide some ideas. – Slaw Dec 24 '22 at 20:21
  • Does this answer your question? [How to walk through Java class resources?](https://stackoverflow.com/questions/749533/how-to-walk-through-java-class-resources) – aSemy Dec 24 '22 at 22:34

0 Answers0