Why is the user.dir system property working in Java?

前端 未结 3 2005
渐次进展
渐次进展 2020-12-16 05:55

Almost every article I read told me that you can\'t have chdir in Java. The accepted answer to this question says you can\'t do it in Java.

However, here\'s s

3条回答
  •  忘掉有多难
    2020-12-16 05:56

    Just because new File(".") gives the desired answer doesn't mean it's doing what you want it to.

    For example, try:

    new FileOutputStream("foo.txt").close();
    

    Where does that end up? On my Windows box, even though new File(".").getAbsolutePath() moves around based on user.dir, foo.txt is always created in the original working directory. It strikes me that setting user.dir such that new File(".") doesn't refer to the current working directory is just asking for trouble.

提交回复
热议问题