1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* Determine if a file can be written This Java example shows how to determine if a particular file has a write permission using canWrite method of Java File class. */ import java.io.*; public class DetermineWriteFile { public static void main(String[] args) { //create file path String filePath = "C:/FileIO/ReadText.txt"; //create File object File file = new File(filePath); /* * To determine whether a particular file can be written use, * boolean canWrite() method of Java File class. * * This method returns true IF AND ONLY IF the file exists and it * can be written (file has a write permission). */ if(file.canWrite()) { System.out.println("File " + file.getPath() +" can be written"); } else { System.out.println("File " + file.getPath() +" can not be written"); } } } /* Output would be File C:FileIOReadText.txt can be written */ |
Add Comment