Why user Do dir system properties work in Java?

Almost every article I've read tells me that you can't create chdir in Java Accepting the answer to this question means that you can't do this in Java

But here are some things I've tried:

geo@code@R_208_2419@:~$java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) Client VM (build 14.0-b16,mixed mode,sharing)

This is the test class I am using:

import java.io.*;

public class Ch {
    public static void main(String[] args) {
        System.out.println(new File(".").getAbsolutePath());
        System.setProperty("user.dir","/media");
        System.out.println(new File(".").getAbsolutePath());
    }
}
geo@code@R_208_2419@:~$pwd
/home/geo
geo@code@R_208_2419@:~$java Ch
/home/geo/.
/media/.

Please explain why this works Can I use it from now on and expect it to work the same way on all platforms?

Solution

Just because of the new file (".) Giving the answer you need doesn't mean it's doing what you want

For example, try:

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

Where will it end up? In my windows box, even if the new file (".") Getabsolutepath() is based on user Dir move, foo Txt is always created in the original working directory Set user Dir to make the new file (".) Not referencing the current working directory is just looking for trouble

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>