Search code examples
javaubuntuiomkdir

mkdirs() not creating any folder with Xubuntu


I am trying to put a file in the directory (The system property user.home)/.plantgrower/logs. The folder .myprogram does not exist, I want to create it with its sub-folders. Here is my code :

File f = new File(System.getProperty("user.home") + File.pathSeparatorChar + ".plantgrower" + File.pathSeparatorChar + "logs");
System.out.println(f.mkdirs());

However, the output is false and the folder is not created. I do not understand why it did that.

Another weird thing : I have set the permissions of user.home (in my case /home/matthieu) for writing and reading for everybody, but f.canWrite() outputs false.


Solution

  • You want File.separatorChar not File.pathSeparatorChar.

    File.separatorChar (/) is used to separate path components, such as:

    /Users/myuser/folder1
    

    File.pathSeparatorChar (:) is used to separate multiple paths, such as:

    /Users/myuser/folder1:/Users/myuser/folder2
    

    The actual characters are OS dependent.