Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java File.isDirectory() returns False for a Directory in Linux

Tags:

java

file-io

Please see code snippet:

File[] additionalFiles = new File(FILE_PATH).listFiles();
boolean isDirectory = file.isDirectory();

I have verified that the directory path is correct, and when I run the code on Windows, the value of isDirectory is true (as it should be). Any suggestions as to why this occurs on Linux (RedHat Enterprise Linux)?

like image 471
shelt536 Avatar asked Mar 15 '10 21:03

shelt536


2 Answers

Symlinks don't read as directories, if I remember correctly. The right way around that is:

 new File(FILE_PATH).getCanonicalFile().isDirectory(); 

(NOTE: Untested, I don't have a linux box to test this on easily).

like image 157
Yishai Avatar answered Oct 21 '22 06:10

Yishai


I experienced this issue once. My case is so funny, I was reading the path from a properties file and that path contained a tab character at the end of the string. That was the reason why the path wasn't recognized as a directory

like image 28
JavaMeat Avatar answered Oct 21 '22 06:10

JavaMeat