Rishab Kaushik
Rishab Kaushik

Reputation: 25

How do i block user access to a directory using java?

Is it possible with java to modify directory permission so that no one can access the content inside that directory.

Upvotes: 0

Views: 1210

Answers (1)

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31299

You can use the method on the File object for this

File dir = new File("my directory");
dir.mkdir();

// Make it not readable, writable and executable for anyone
dir.setExecutable(false, false);
dir.setReadable(false, false);
dir.setWritable(false, false);

// Make it readable, writable and executable for the owner only (see
// second parameter "ownerOnly" to these calls)
dir.setExecutable(true, true);
dir.setReadable(true, true);
dir.setWritable(true, true);

The Java NIO API has more fine-grained controls to do the same, such as

Upvotes: 1

Related Questions