Java Example - Setting a File to Read-Only
The following example demonstrates how to use the File class's file.setReadOnly() and file.canWrite() methods to set a file to read-only:
Main.java File
import
java
.
io
.
File
;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
File
file
=
new
File
(
"
C:/java.txt
"
)
;
System
.
out
.
println
(
file
.
setReadOnly
(
)
)
;
System
.
out
.
println
(
file
.
canWrite
(
)
)
;
}
}
The output of the above code is:
true
false
Java Examples