Check read,write,execute permission & hidden attribute of file in java

By | December 18, 2016
  1. Given a file in java, check read, write & execute permissions of a file.
    • We would like to check whether file has read only or read write permissions.
  2. We will also check whether file is hidden or not.
  3. We will use File class to check permission & hidden attributes of a file.
No.File Api Description
1boolean canRead()Tests whether the application can read the file denoted by this abstract pathname.
2boolean canWrite()Tests whether the application can modify the file denoted by this abstract pathname.
3boolean canExecute()Tests whether the application can execute the file denoted by this abstract pathname.
4boolean isHidden()Tests whether the file named by this abstract pathname is a hidden file.
  • We have kept three files in a input directory, namely readOnlyFile.txt, readWriteFile.txt & hiddenFile.txt.
  • We will check read, read write, execute permissions of a file.
    • We will also check whether given file is hidden or not.

Program: check read,write permission & hidden attribute of file in java

 package org.learn.io;

import java.io.File;
import java.io.IOException;

public class CheckFilePermissions {
    public static void main(String[] args) throws IOException {

        String inputDirectory = "C:\\ioOPerations";
        String readOnlyFile = "readOnlyFile.txt";
        String readWriteFile = "readWriteFile.txt";
        String hiddenFile = "hiddenFile.txt";

        boolean isReadOnly = false;
        boolean isReadWrite = false;
        boolean isHidden = false;
        boolean isCanExecute =false;

        //Checking permission for read only file
        File fileReadOnly = new File (inputDirectory + File.separator + readOnlyFile);
        isReadOnly = fileReadOnly.canRead();
        isReadWrite = fileReadOnly.canWrite();
        isHidden = fileReadOnly.isHidden();
        isCanExecute = fileReadOnly.canExecute();

        System.out.printf("1. File %s is ReadOnly=%b, ReadWrite=%b, Hidden=%b, canExecute=%b",
                fileReadOnly.getCanonicalPath(),isReadOnly,isReadWrite, isHidden,
                isCanExecute);

        File fileReadWrite = new File (inputDirectory + File.separator + readWriteFile);
        isReadOnly = fileReadWrite.canRead();
        isReadWrite = fileReadWrite.canWrite();
        isHidden = fileReadWrite.isHidden();

        System.out.printf("\n2. File %s is ReadOnly=%b, ReadWrite=%b, Hidden=%b, canExecute=%b",
                fileReadWrite.getCanonicalPath(),isReadOnly,isReadWrite, isHidden,
                isCanExecute);

        File fileHidden = new File (inputDirectory + File.separator + hiddenFile);
        isReadOnly = fileHidden.canRead();
        isReadWrite = fileHidden.canWrite();
        isHidden = fileHidden.isHidden();

        System.out.printf("\n3. File %s is ReadOnly=%b, ReadWrite=%b, Hidden=%b, canExecute=%b",
                fileHidden.getCanonicalPath(),isReadOnly,isReadWrite, isHidden,
                isCanExecute);
    }
}

Output: check read,write permission & hidden attribute of file in java

1. File C:\ioOperations\readOnlyFile.txt is ReadOnly=true, ReadWrite=false, Hidden=false, canExecute=true
2. File C:\ioOperations\readWriteFile.txt is ReadOnly=true, ReadWrite=true, Hidden=false, canExecute=true
3. File C:\ioOperations\HiddenFile.txt is ReadOnly=true, ReadWrite=true, Hidden=true, canExecute=true