user.dir property
Kresten Krab Thorup
krab@eos.dk
Sat Apr 1 00:00:00 GMT 2000
Here's a question.
I'm trying to set the property user.dir and thereby effectively cd'ing
to a given directory. The following piece of code tries do do that, but
fails on JDK1.2.2. The code does the following
create file "/tmp/foo.bar"
set user.dir = "/tmp"
read "foo.bar"
Obviously, this is only interesting if the original value of "user.dir"
is different from "/tmp".
The question is if this ought to be possible. As I read the javadoc for
java.lang.File, it seems that it should.
Any coments?
--Kresten
----------------------------------------------------------------------
import java.io.*;
public class TestUserDir
{
public static void main (String[] args)
throws IOException
{
String user_dir = System.getProperty ("user.dir");
System.out.println ("user.dir = "+user_dir);
File tmp = File.createTempFile ("foo", "bar");
System.out.println ("tmpfile = "+tmp);
File tmp2 = new File(tmp.getAbsolutePath ());
System.out.println ("abstmp = "+tmp2);
String parent = tmp2.getParent ();
String file = tmp2.getName ();
System.out.println ("parent = "+parent);
System.out.println ("file = "+file);
try {
FileOutputStream fo = new FileOutputStream (tmp2);
fo.write (27);
fo.close ();
} finally {
System.out.println ("wrote one byte to "+tmp2);
}
System.setProperty ("user.dir", parent);
System.out.println ("cd'ed to "+parent);
try {
FileInputStream fi = new FileInputStream (file);
int b = fi.read ();
if (b == 27)
{
System.out.println ("sucessfully read from "+file);
}
} catch (IOException e) {
System.out.println ("** failed to read "+file);
throw e;
}
}
}
--
Kresten Krab Thorup, Director of Research
Eastfork Object Space (EOS), Margrethepladsen 3, 8000 ÃÂ
rhus C, Denmark
Tel: +45 8732 8787 / Fax: +45 8732 8788 / Mob: +45 2343 4626
More information about the Java
mailing list