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
?
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
?
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.
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:
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)
Check the permissions of the parent directories of this file. Some of these directories may not have execute permission for the current user.
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.
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.
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
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".
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", " "));