The setReadOnly() method from Java IO sets the file or directory to be read-only. After running that method only read operations will be allowed.
2. Method signature
method does not take any parameter
true - true if the operation succeeded; false otherwise
SecurityException - when we do not have access to the file or directory
3.1. Code that sets the read-only flag for a file and try to write some text to it
After changing the read-only flag when we try to write some text to the file we faced an exception and that is, of course, expected behavior.
In this article, we presented the setReadOnly flag that marks the file or directory so that only read operation is allowed. After invoking this method the file or directory is guaranteed not to change until it is either deleted or marked to allow write access.