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





java.nio.file.attribute.DosFileAttributes's isReadOnly() method can be used for finding whether file is readOnly or not.
Method returns true  - if file is ReadOnly
Method returns false - if file isn't ReadOnly



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


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.DosFileAttributes;
/** JavaMadeSoEasy.com */
public class IsFileReadOnlyJava7 {
   public static void main(String... args) throws IOException {
         
          String fileName = "c:/myFile.txt";
          // create java.nio.file.Path
          Path path = Paths.get(fileName);
          // create DosFileAttributeView and DosFileAttributes
          DosFileAttributeView basicFileAttributeView = Files
                       .getFileAttributeView(path, DosFileAttributeView.class);
          DosFileAttributes basicFileAttributes = basicFileAttributeView
                       .readAttributes();
          //Is ReadOnly?
          boolean isFileReadOnly = basicFileAttributes.isReadOnly();
         
          if(isFileReadOnly)
                 System.out.println(fileName+" is ReadOnly ");
          else
                 System.out.println(fileName+" isn't ReadOnly ");
  
  
   }
}
/* OUTPUT
c:/myFile.txt isn't hidden
*/



RELATED LINKS>

Create File using createNewFile() method in java file IO


Program to Delete a file - 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 creation, last modification and last accessed date of file in date, month and year in java7 using java.nio.file



Program to Make a file READ ONLY in java

Program to Make a file readable and writable in java



No comments:

Post a Comment