I’m such a moron.
You end up with, hairpulling results like this:
writing to rel file: ./hello.txt
writing to abs file: M:\java\workspace\Stuff\new-work-dir\.\hello.txt
file path 1: M:\java\workspace\Stuff\new-work-dir\.\hello.txt
file path 2: M:\java\workspace\Stuff\new-work-dir\hello.txt
file exists at path 1: true
file exists at path 2: false
actual path of file: M:\java\workspace\Stuff\hello.txt
actual path of file: M:\java\workspace\Stuff\new-work-dir\..\hello.txt
See for yourself…
public static void test() throws IOException
{
File newWorkingDirectory = new File("./new-work-dir/");
newWorkingDirectory.mkdir();
// change working directory
{
String dir = newWorkingDirectory.getCanonicalFile().getAbsolutePath();
System.out.println("Checking work-directory: " + new File("./").getAbsolutePath());
System.out.println("=> Setting work-directory to: " + dir);
System.out.println("=> System.setProperty(\"user.dir\", \"" + dir + "\");");
System.setProperty("user.dir", dir);
System.out.println("Checking work-directory: " + new File("./").getAbsolutePath());
}
System.out.println();
// write file
File f = new File("./hello.txt");
{
System.out.println("writing to file: " + f.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(f);
fos.write((byte) '3');
fos.flush();
fos.close();
}
System.out.println();
// check, check, double-check
{
System.out.println("file path 1: " + f.getAbsolutePath());
System.out.println("file path 2: " + f.getCanonicalFile().getAbsolutePath());
System.out.println("file exists at path 1: " + f.exists());
System.out.println("file exists at path 2: " + f.getCanonicalFile().exists());
System.out.println();
System.out.println("where the file really ended up: " + new File("../hello.txt").getAbsolutePath());
System.out.println("where the file really ended up: " + new File("../hello.txt").getCanonicalPath());
}
System.out.println();
}
