0

I've tried this:

File file = new File(path);
file.setReadable(false);
System.out.println(file.canRead()); // returns true

How can I make a file unreadable with Java code ?

Kaytus
  • 9
  • 1
  • try this: https://stackoverflow.com/questions/664432/how-do-i-programmatically-change-file-permissions – Marc Stroebel Aug 31 '22 at 07:55
  • Note 2 things: 1) `setReadable(false)` calls `setReadable(false,true)` so the owner (which might be you) would still be able to read - and 2) `canRead()` might still return true if the JVM is started with elevated privileges (see Javadoc on that method). No. 2 probably doesn't apply in your case but no. 1 likely does. – Thomas Aug 31 '22 at 08:55
  • I work on windows, so option with `setPosixFilePermissions()` doesn't work for me. Also even `setReadable(false, false)` returns true in `canRead()`. `Files.isReadable(path)` doesn't work either. – Kaytus Aug 31 '22 at 09:24
  • AclFileAttributeView from https://stackoverflow.com/questions/664432/how-do-i-programmatically-change-file-permissions finally helped me. Thanks ! – Kaytus Aug 31 '22 at 11:05

0 Answers0