I have the following File object pointing to a directory via symbolic link,
File directory = new File("/path/symlink/foo/bar");
String[] files = directory.listFiles();
listFiles() returns null, is this because of the symlink? if yes, how will I go about this if I really want to list the files in bar using the path that contains a symlink?
Ln Command to Create Symbolic Links By default, the ln command creates a hard link. Use the -s option to create a soft (symbolic) link. The -f option will force the command to overwrite a file that already exists.
You can create a symlink (symbolic) by using the ln command in the command line. Symbolic links are useful because they act as shortcuts to a file or directory. In this article, I will go over how to use the ln command to create a symlink to a file or directory.
You can create a symlink to practically anything, including things that don't exist.
From this a symlink is merely another name for a named path to either a file or a folder. In other words they only point one way.
According to what I've seen while Googling this puzzling behavior, Java requires that you call .getCanonicalFile()
on a File
whose path contains a link before you can use it in other file operations.
So:
File directory = new File("/path/symlink/foo/bar").getCanonicalFile();
String[] files = directory.listFiles();
You could read the Symbolic LINK
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With