javadirectorymkdirs

Why my code creates directory on windows but not on linux JAVA


I have mkdirs code like that;

File dir = new File ("/Mydir/");
            if(dir.exists()==false) {
                dir.mkdirs();
            }

it is normal working and create directory on windows but not working on linux..


Solution

  • /MyDir/ is a reference to a directory inside root-dir / - you'll need root privileges to write there.

    For creating a directory inside user home you could use "~/MyDir" in linux - but that will not work again in Windows.

    If you're forced to use old-style File operations you could go

     new File(new File(System.getProperty("user.home")), "MyDir").mkdir();
    

    Better yet would be to invoke

     Files.createDirectories(
        Paths.get(System.getProperty("user.home"), "MyDir"));