问题
Does anyone encountered the case when a java.io.File
doesn't exist but the calling getAbsoluteFile()
method on the file instance returns a file that does exist. And why this happen?
Note
- I am on Linux plus Oracle Java runtime with version
1.7.0_95-b00
- I've passed in a JVM option
-Duser.dir=/path/to/somewhere
when launching the tomcat instance
回答1:
You should probably not mess around with user.dir
. Rather, change to that directory before launching the Tomcat.
File.getAbsoluteFile()
is assuming that user.dir
is the directory you are really in, but you aren’t.
Based on your observation, I think File.exists()
is mapped directly to stat
on the operating system level. And File.getAbsoluteFile()
is just new File(System.getProperty("user.dir"), getPath())
.
Under the assumption that user.dir
is the operating system’s current working directory, these two implementations are correct and reasonable. But in your case, they differ, and this (assumed) implementation can explain what you are experiencing.
来源:https://stackoverflow.com/questions/36880692/java-file-does-not-exists-but-file-getabsolutefile-exists