Java File不存在但File.getAbsoluteFile()存在 [英] Java File does not exists but File.getAbsoluteFile() exists
问题描述
当 java.io.File
不存在但调用 getAbsoluteFile()
文件实例上的方法返回确实存在的文件。为什么会这样?
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?
注意
- 我在Linux上加上Oracle Java运行时版本
1.7.0_95-b00
- 当
启动tomcat实例时,我传入了JVM选项-Duser.dir = / path / to / somewhere
- 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
推荐答案
您可能不应该使用 user.dir
。而是在启动Tomcat之前更改到该目录。
You should probably not mess around with user.dir
. Rather, change to that directory before launching the Tomcat.
File.getAbsoluteFile()
假设 user.dir
是你真正的目录,但你不是。
File.getAbsoluteFile()
is assuming that user.dir
is the directory you are really in, but you aren’t.
基于根据您的观察,我认为 File.exists()
直接映射到操作系统级别的 stat
。并且 File.getAbsoluteFile()
只是新文件(System.getProperty(user.dir),getPath())
。
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())
.
假设 user.dir
是操作系统的当前工作目录,这两个实现是正确合理。但在你的情况下,它们是不同的,这个(假定的)实现可以解释你正在经历的事情。
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.
这篇关于Java File不存在但File.getAbsoluteFile()存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!