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
Parameters:
method does not take any parameter
Returns
true - true if the operation succeeded; false otherwise
Throws
SecurityException - when we do not have access to the file or directory
3. Examples
3.1. Code that sets the read-only flag for a file and try to write some text to it
The output:
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.
4. Conclusion
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.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}