1

What I am trying to do is to create a new File with relative path in my user-directory after I changed it. To change the user-directory I used System.setProperty("user.dir", "/data");, then I created a file-object with File f2 = new File("f2"); and created the empty file on my filesystem with f2.createNewFile();. After this I expected the file to appear in /data/f2, and this is what f2.getAbsolutePath() tells me. But, confusingly, the file appears in the "old, initial" userDir.

Here is my Test:

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

public class Main {

    private static FilenameFilter filter = new FilenameFilter() {

        public boolean accept(File dir, String name) {
            return (name.startsWith("f")) ? true : false;
        }
    };

    public static void main(String[] args) throws IOException {
        String userDirPropertyName = "user.dir";
        File initialUserDir = new File(System.getProperty(userDirPropertyName));

        System.out.println("files in " + initialUserDir.getAbsolutePath() + ":");
        for (File file : initialUserDir.listFiles(filter)) {
            System.out.println("  - " + file.getAbsoluteFile());
        }

        System.out.println("initial userDir = " + System.getProperty(userDirPropertyName));
        File f1 = new File("f1");
        f1.createNewFile();
        System.out.println("f1.getAbsolutePath() = " + f1.getAbsolutePath());
        System.out.println("getCanonicalPath() for . = " + new File(".").getCanonicalPath());

        System.setProperty(userDirPropertyName, "/data");

        System.out.println("changed userDir = " + System.getProperty(userDirPropertyName));
        File f2 = new File("f2");
        f2.createNewFile();
        System.out.println("f2.getAbsolutePath() = " + f2.getAbsolutePath());
        System.out.println("getCanonicalPath() for . = " + new File(".").getCanonicalPath());


        System.out.println("files in " + initialUserDir.getAbsolutePath() + ":");
        for (File file : initialUserDir.listFiles(filter)) {
            System.out.println("  - " + file.getAbsoluteFile());
        }
    }
}

This the output, that I get:

files in /home/pps/NetBeansProjects/UserDirTest:  
initial userDir = /home/pps/NetBeansProjects/UserDirTest  
f1.getAbsolutePath() = /home/pps/NetBeansProjects/UserDirTest/f1  
getCanonicalPath() for . = /home/pps/NetBeansProjects/UserDirTest  
changed userDir = /data  
f2.getAbsolutePath() = /data/f2  
getCanonicalPath() for . = /data  
files in /home/pps/NetBeansProjects/UserDirTest:  
  - /home/pps/NetBeansProjects/UserDirTest/f1  
  - /home/pps/NetBeansProjects/UserDirTest/f2

f1 and f2 appear in the same directory though I changed the user.dir in between?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ralfuslongus
  • 11
  • 1
  • 2
  • possible duplicate of [Java : File.exists() inconsistencies when setting "user.dir"](http://stackoverflow.com/questions/2275362/java-file-exists-inconsistencies-when-setting-user-dir) – Joachim Sauer Feb 17 '11 at 14:35
  • Yes, strange thing, i guess i got to use absolute path. – ralfuslongus Feb 18 '11 at 07:51

3 Answers3

1

I have just reproduced your scenario. I think that your mistake is that your are trying to use system property to change current working directory. Ability to retrieve this directory from system property is just a convenience method. If you change the property the directory itself is not being changed.

The solution is to create directory your want using File.mkdir() or File.mkdirs() and then use new File(myDir, fileName) where myDir is your new directory.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • Thank you, i guess i got to use it like this. – ralfuslongus Feb 18 '11 at 07:53
  • @alexr at some point as this test points out, changing user.dir IS changing some stuff and not others(it is not consistent) https://stackoverflow.com/questions/45130661/user-dir-property-broken-on-osx-jdk-1-8-0-111-how-about-other-os-versions – Dean Hiller Jul 16 '17 at 16:11
1

Setting of the property user.dir is not supported and leads to all kinds of strange effects.

Community
  • 1
  • 1
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
0

There is no way to change the current directory in Java.

When you change the system property, the property is changed, but not the actual working directory, used by native code to create the file. getAbsolutePath, however, uses the current value of the user.dir system directory to compose the absolute path.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255