I have the problem of wanting to list all files in a directory. The directory in the IDE is src/main/resources/commands/cirno/
, in the jar, it's commands/cirno/
:
/
commands/
cirno/
file1
file2
What I tried is opening an InputStream
and listing the lines with a BufferedReader
:
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("commands/cirno/");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
ArrayList<String> strings = new ArrayList<>();
br.lines().forEach(strings::add);
strings.forEach(System.out::println);
This works in the IDE, the console output is as follows:
file1
file2
The problem I is that it stops working when packaged as .jar
. It still works when I open a single file, but when I try to open a folder, the returned InputStream is empty. It is not null
, as is == null
returns false, but br.readLine()
returns an empty String.