Вопрос

I see the question "what user.dir means?", how is it identified, why it does not work properly, how to change it. But I do not see the basic question: why do we need "user.dir" in the first place, after all operating systems support "." for current directory?

I see that user.dir = absolute path to the current directory whareas . is relative. Is it just a shorthand for File(".").getAbsoluteFile().getParentFile().getName()?

Это было полезно?

Решение 2

I think that the purpose of "user.dir" property is the same as pwd command in Linux or GetCurrentDirectory in Windows. I think it is more clear this way because we clearly use pwd when we want to get the (absolute) path of the current directory. In contrast, . is a shorthand for the current dir when you are not interested to know it but, rather, just tell the system that we want to use the current path. It is reasonable to use the shorthand in this case. First means current path relative to the root, second -- current path relative to the current path.

The difference may be important when you communicate between processes. If you pass a path to another process in your system, it may have a different current dir and, thus, you better prefer the absolute path. On the other hand, if you pass a path to another system, then relative path may be preferred.

Другие советы

Because the current directory can change, based on a program navigating through the filesystem. The directory the program started from cannot change.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top