Program to Make a file readable and writable in java



java.io.File's setWritable(true) method can be used to make file readable and writable

java.io.File's canWrite() method can be used to find whether file is READ ONLY or not
Method returns true  - if file is readable and writable.
Method returns false - if file is READ ONLY.




Program to Make a file readable and writable in java >

import java.io.File;
/** JavaMadeSoEasy.com */
public class WritableFile {
   public static void main(String... args) {
          String fileName = "c:/myFile.txt";
          File file = new File(fileName);
         
          //First, make the file READ ONLY.
          System.out.println("------Make " + fileName + " READ ONLY ------");
          file.setReadOnly();
          if (file.canWrite()) {
                 System.out.println(fileName + " is readable and writable");
          } else {
                 System.out.println(fileName + " is READ ONLY");
          }
         
          //Now, make the file readable and writable
          System.out.println("------Make " + fileName + " readable and writable ------");
          file.setWritable(true);
          if (file.canWrite()) {
                 System.out.println(fileName + " is now readable and writable");
          } else {
                 System.out.println(fileName + " is READ ONLY");
          }
         
   }
}
/* OUTPUT
------Make c:/myFile.txt READ ONLY ------
c:/myFile.txt is READ ONLY
------Make c:/myFile.txt readable and writable ------
c:/myFile.txt is now readable and writable
*/



RELATED LINKS>

Program to Create Directory - Single and multiple (i.e. parent and child directories) in java file IO


Program to Identify File or Directory - in java file IO | And in java 7 using java.nio.file


2 Program to Delete a Directory


Copy a Directory


Traverse a Directory (all sub-directories and files) in 2 ways




No comments:

Post a Comment