change directory
1 posts in topic
Flat View  Flat View

Posted By:   pooh_c
Posted On:   Wednesday, August 28, 2002 10:52 AM

How to do " cd" in Java?

thanks for any help

Re: change directory

Posted By:   Rob_Eamon  
Posted On:   Wednesday, August 28, 2002 12:48 PM

http://www.jguru.com/faq/view.jsp?EID=203366 has some info on this. I can confirm that doing a setProperty() on user.dir does not change the cwd--it only affects path resolution when using getCanonicalPath() and getAbsolutePath(). The following shows the behavior:

System.out.println("cwd=" + System.getProperty("user.dir"));
File f = new File(System.getProperty("user.dir"), "bogus.txt");
System.out.println("Created: " + f.getAbsolutePath());

System.setProperty("user.dir", "e:/tmp");
System.out.println("cwd=" + System.getProperty("user.dir"));
File f2= new File("bogus2.txt");
System.out.println("Created: " + f2.getAbsolutePath());

The last println will indicate that the file was created in e:/tmp but it is not. Both files are created in the original cwd when the JVM was started. You can try to cheat and use user.dir as a way to store a pseudo-cwd but undoubtedly you'll get bit when you forget about this trick being in place and files get created where you don't expect them to. My advice is to heed the advice in the FAQ and use absolute file references instead of relative.
About | Sitemap | Contact