15

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?

user760658
  • 265
  • 1
  • 3
  • 12
  • 6
    check File.exists() and File.canRead() – HectorLector Feb 13 '13 at 12:19
  • 1
    May be path you are providing is incorrect . – Achintya Jha Feb 13 '13 at 12:22
  • 3
    `File.isDirectory()` **should** return **false** on files. Another thing, what is that `.` after the file extension? – Maroun Feb 13 '13 at 12:22
  • 1
    Path is correct, i am not able to read file name – user760658 Feb 13 '13 at 12:24
  • file name is 21750_070503 - Sondengängigkeit.xls – user760658 Feb 13 '13 at 12:25
  • 1
    It may be the umlaut in your file name. See: http://stackoverflow.com/questions/1455369/java-io-filenotfoundexception-when-retrieving-a-url-with-umlauts-in-the-filename – MusiGenesis Feb 13 '13 at 12:26
  • 1
    Did you try to change the file name, and check again? And make sure that your OS doesn't hide the extension, so maybe the real file name is `21750_070503 - Sondengängigkeit.xls.xls` in this case.. – Maroun Feb 13 '13 at 12:30
  • It could be the spaces in the name. I know command line doesn't handle spaces without a '\' in front, so it depends how isFile() works. Try renaming without spaces and see if it works. – David K Feb 13 '13 at 13:19
  • possible duplicate of [File returns always false for isDirectory and isFile in Java](http://stackoverflow.com/questions/9775659/file-returns-always-false-for-isdirectory-and-isfile-in-java) – Shashank Shekhar Oct 31 '13 at 18:40
  • could also be a symlink ? at first glance the ä seems possibly problematic. did you try another file name? – vikingsteve Apr 12 '18 at 13:39

8 Answers8

7

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.

Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66
  • what is system-dependent criteria? Even .exits() is returning false – user760658 Feb 13 '13 at 12:45
  • I was trying to move my file in my Android app. The `newPath` argument passed to `renameTo()` was actually a folder (which got created during testing). Removing the folder solved the problem. It's a poor design that `renameTo()` doesn't throw any sort of exception! – Sufian Aug 22 '14 at 06:02
6

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:

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)

Community
  • 1
  • 1
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
3

Check the permissions of the parent directories of this file. Some of these directories may not have execute permission for the current user.

  • The execute bit of directory allows the affected user to enter it and access files and directories inside
stan
  • 984
  • 10
  • 15
2

I know that this question was asked five years ago, in fact, I got to it because I had the same problem, I am creating a List of all the files in a given path, like this:

File files = Paths.get(path).toFile();      
List<String> filenames = Arrays.asList(files.list());

The thing is, that path contains a directory which is called testing_testing, which is being returned as part of the list.

Then when I do the following test:

for (String filename : filenames) {
    if (Files.isDirectory(Paths.get(filename))) {
        System.out.println(filename + " is a directory.");
    } else {
        if(filename.equals("testing_testing")) {
            System.out.println("Is this a directory?: " + Files.isDirectory(Paths.get(filename)));
            System.out.println("Does the file exists?: " + Files.exists(Paths.get(filename)));
            System.out.println("Is this a regular file?: " + Files.isRegularFile(Paths.get(filename)));
            System.out.println("Is this a symbolic link?: " + Files.isSymbolicLink(Paths.get(filename)));                       
        }                   
    }
}

It is returning false for Files.isDirectory() and for Files.exists().

Tinkering around for a bit, I noticed that I was only getting the filenames, without the full path to them, which meant that I was only passing testing_testing to Paths.get() instead of passing the full path to get to it, that's why it didn't exists and returned false for both scenarios.

I changed the code to:

Paths.get("C:\test", filename);

And now the tests return the proper values. I don't know if you've already figured this out, because it's been five years since you asked. But for anyone with the same problem, make sure that you're passing the correct path to your files, and then try the other things suggested in previous answers on this same question.

Anubis Lockward
  • 115
  • 2
  • 11
  • 4
    I know you are not supposed to thank, but goddamn thank you. I was about to throw the whole damn code away and just go outside into the sun to live life. Your's is the only real answer to the question. – fandango Feb 09 '19 at 11:34
  • @fandango glad I could help. – Anubis Lockward Feb 12 '19 at 01:15
0

I've also had problems with file.isFile() returning false on files, presumably because the file is not "regular", as noted in other responses to this question. As a workaround, I use file.listFiles() != null, which seems to provide the functionality I need. According to the Java File API:

If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned.

Stemkoski
  • 8,936
  • 3
  • 47
  • 61
0

I got same error when i was testing isFile() on a .txt file. The problem was the file i created had something.txt with .txt on the name. Then i renamed something.txt to something

I was really mad with myself

David Concha
  • 143
  • 1
  • 7
0

The character encoding used by Java in your case is different from the character encoding in the source file, so the symbol "ä" in the file name cannot be properly decoded by Java, resulting in a different file name. That's why Java cannot find the file. Therefore, the file manipulation functions over this file return "False".

As the safest way to work properly in different build environments, to avoid setting Java character encoding option and also make handling source files easier, use US-ASCII only (7-bit) characters in the source code. As for the other characters, use their Unicode numbers, e.g., instead of "ä" use "\u00e4". So, your filename would become "aaa\u00e4aa.xls".

Maxim Masiutin
  • 3,991
  • 4
  • 55
  • 72
0

I've had this issue several times and if everything has been tried then it might be that you have issues with pathing. Any space character is replaced by %20, and it results in an issue.

Therefore, whereas this doesn't work:

File file = new File(Objects.requireNonNull(getClass().getResource(/path/to/file).getPath());

This actually does:

File file = new File(Objects.requireNonNull(getClass().getResource(/path/to/file).getPath().replace("%20", " "));