Create folders programmatically along with permissions using java to save content to that location

user850234 picture user850234 · Jan 14, 2013 · Viewed 14.8k times · Source

I have xampp installed in my windows system and lampp installed in my linux system. I want to create folder in the location "http://localhost/" using java. I have done the following :

dirName = new File("http://localhost/"+name);
if(!dirName.exists()) {
    dirName.mkdir();
}

Is it possible to do? The idea is to download some files to that location programatically. Download is working but how do I create folders so that I can access it via http://example.com/name. This is required to keep track of the user related content. I have access to the apache web server with lampp already installed. How can I create folders and save the downloads to that folder with programmatically assigning the permissions to the folder and contents within it so that saved contents can be download from there using wget method.

Answer

fge picture fge · Jan 14, 2013

Don't use the File API. It is ridden with misbehavior for serious filesystem work.

For instance, if a directory creation fails, the .mkdir() method returns... A boolean! No exception is thrown.

Use Files instead.

For instance, to create a directory:

// Throws exception on failure
Files.createDirectory(Paths.get("/the/path"), 
      PosixFilePermissions.asFileAttribute(      
         PosixFilePermissions.fromString("rwxr-x---")
      ));