Java File does not exists but File.getAbsoluteFile() exists

随声附和 提交于 2019-12-20 04:27:31

问题


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

  1. I am on Linux plus Oracle Java runtime with version 1.7.0_95-b00
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!