Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

both File.isFile() and File.isDirectory() is returning false

Tags:

java

file

I have a file with name "aaaäaa.xls"

For this, File.isFile() and File.isDirectory() is returning false? why it is returning false in Linux?

like image 265
user760658 Avatar asked Feb 13 '13 12:02

user760658


2 Answers

Please try the following code example

if(!pFile.exists()){
   throw new FileNotFoundException();
}

boolean isDir = pFile.isDirectory();

boolean isFile = pfile.isFile();

the file is not a file

if it is not a directory and, in addition, satisfies other system-dependent criteria

if the exception is thrown, you have to check the file path.

like image 171
Markus Lausberg Avatar answered Oct 27 '22 17:10

Markus Lausberg


According to the documentation:

public boolean isFile()

Returns: true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise.

From this basis, your file either doesn't exist or is not a normal file.

Possible reasons of the 1st:

  1. file doesn't exist;
  2. file can't be accessed;
  3. file name is mistyped;
  4. the character encoding used in your program isn't the same as that used when you created the file.

Possible reasons of the 2nd:

  • it's not a regular file.

Or it's a bug in JVM. It's also possible though unlikely. For example at once I had problems with an exclamation mark in path names - Bug 4523159.

If you want to access the file in any way, consider calling dir.listFiles() and work with its return value.


(answer is partially based on this thread)

like image 6
naXa Avatar answered Oct 27 '22 19:10

naXa