Program to Make a file READ ONLY in java



java.io.File's setReadOnly() method can be used to make file READ ONLY

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 READ ONLY in java >

import java.io.File;
/** JavaMadeSoEasy.com */
public class ReadOnlyFile {
   public static void main(String... args) {
          String fileName = "c:/myFile.txt";
          File file = new File(fileName);
          if (file.canWrite()) {
                 System.out.println(fileName + " is readable and writable");
          } else {
                 System.out.println(fileName + " is READ ONLY");
          }
          // Now, 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 now READ ONLY");
          }
   }
}
/* OUTPUT
c:/myFile.txt is readable and writable
------Make c:/myFile.txt READ ONLY ------
c:/myFile.txt is now READ ONLY
*/



RELATED LINKS>

Create File using createNewFile() method in java file IO


Copy a file in java 2 in ways - use org.apache.commons.io.FileUtils's copyDirectory(sourceDirectory, destDirectory)


Program to Move a file - in java file IO


Program to Delete a file - in java file IO


Program to Append to file using FileOutputStream in java file IO


Program to Hide and unHide File or Directory (by executing CMD commands in java program) till java6 - file IO

Program to Find file is Read only or not in java 7

Program to Make a file readable and writable in java



No comments:

Post a Comment