- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to change a file attribute to writable
Let’s say our file is “input.txt”, which is set read-only −
File myFile = new File("input.txt"); myFile.createNewFile(); myFile.setReadOnly();
Now, set the above file to writable −
myFile.setWritable(true);
After that, you can use canWrite() to check whether the file is writable or not.
Example
import java.io.File; public class Demo { public static void main(String[] args) throws Exception { File myFile = new File("input.txt"); myFile.createNewFile(); myFile.setReadOnly(); if (myFile.canWrite()) { System.out.println("Writable!"); } else { System.out.println("Read only mode!"); } // set file to writable myFile.setWritable(true); if (myFile.canWrite()) { System.out.println("Writable!"); } else { System.out.println("Read only mode!"); } } }
Output
Read only mode! Writable!
- Related Articles
- Change a file attribute to writable in Java
- Change a file attribute to read only in Java
- Golang Program to check a file has writable permission or not
- Create a file and change its attribute to read-only in Java
- How to check if a file is readable, writable, or, executable in Java?
- Java Program to Save a String to a File
- Java Program to write int array to a file
- Java Program to rename a file or directory
- Java program to delete certain text from a file
- C program to change the file name using rename() function
- Java Program to write an array of strings to a file
- Java Program to delete file or directory
- Java Program to delete a file or directory when the program ends
- Java Program to Append Text to an Existing File
- Java Program to Create String from Contents of a File

Advertisements