
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Write Permissions for a File in Java
The method java.io.File.canWrite() is used to check whether the file can be written to in Java. This method returns true if the file specified by the abstract path name can be written to by an application and false otherwise.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo { public static void main(String[] args) { try { File file = new File("demo1.txt"); file.createNewFile(); System.out.println("The file can be written to? " + file.canWrite()); } catch(Exception e) { e.printStackTrace(); } } }
The output of the above program is as follows −
Output
The file can be written to? true
Now let us understand the above program.
The method java.io.File.canWrite() is used to check whether the file can be written to and the boolean value that is returned by the method is printed. A code snippet that demonstrates this is given as follows −
try { File file = new File("demo1.txt"); file.createNewFile(); System.out.println("The file can be written to? " + file.canWrite()); } catch(Exception e) { e.printStackTrace(); }
Advertisements