- 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!
Advertisements