I need to get a list of all files that exist in a directory including broken symlinks and other entries that would be considered invalid by File.listFiles()
.
I tried multiple different ways of listing files in Java and all of them seem to discard "invalid" files.
To reproduce this issue:
# Create a symlink to a nonexistent path
ln -s /nonexistent/path $PWD/broken_link
# Create a valid symlink
ln -s /path/to/existing/file $PWD/working_link
# Create a normal file
touch empty_file
var files = new File(".").listFiles();
Arrays.stream(files).forEach(file -> {
System.out.println(file.getName();
});
Expected output (in no particular order):
broken_link
working_link
empty_file
Actual output:
working_link
empty_file
Considering the nature of Java, a multiplatform solution would be ideal but reallistically I only need this to work on POSIX systems (namely macOS and Linux) but I would like to avoid any kind of external dependencies (such as parsing th eoutput of ls
, using JNI, etc).