Java File canWrite() Method
Example
Check if a file can be changed:
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
File myFile = new File("filename.txt");
myFile.createNewFile();
System.out.println(myFile.canWrite());
}
}
Definition and Usage
The canWrite() method checks if the program is allowed to change (write to) the file.
It returns false if the file does not exist, or if the file is read-only (it can be read, but not changed).
Tip: Use the setReadOnly() method to make a file read-only.
Syntax
public boolean canWrite()
Parameter Values
None.
Technical Details
| Returns: | true if the file exists and can be changed, otherwise false. |
|---|
Related Pages
❮ File Methods