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

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


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 failureFiles.createDirectory(Paths.get("/the/path"),       PosixFilePermissions.asFileAttribute(               PosixFilePermissions.fromString("rwxr-x---")      ));


Use Java Files with PosixPermission.[Note- PosixPermission is not supported in Windows]

Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxrwxrwx");Files.createDirectories(path, PosixFilePermissions.asFileAttribute(perms));


In Java you can create files in any writeable directory on your system by doing something like:

File file1 = new File("/var/www/newDirectory/");file1.mkdirs();

Then to create a file in that directory you can do something like this:

File file2 = new File(file1.getAbsolutePath() + "newFile.txt"); // You may need to add a "File.seperator()" after the "file1.getAbsolutePath()" if the trailing "/" isn't includedif (file2.exists() == false) {    file2.createNewFile();}

To ensure that your file is readable to the public you should add read permissions to the file:

file2.setReadable(true, false);

In Apache you can set up a virtual host that points to the directory where you would like to make files available from. By default on debian linux it is /var/www.