Sunday, 11 June 2017

Java Tutorial : Java IO (Java File - How to set the file permission) ~ foundjava


Click here to watch in Youtube :

Click the below Image to Enlarge
Java Tutorial : Java IO (Java File - How to set the file permission) 
FileDemo.java
import java.io.File;

public class FileDemo
{

    public static void main(String[] args)
    {
        File file = new File("/home/start.sh");

        System.out.println("Is Execute allow : " + file.canExecute());
        System.out.println("Is Write allow : " + file.canWrite());
        System.out.println("Is Read allow : " + file.canRead());

        file.setExecutable(false);
        file.setReadable(false);
        file.setWritable(false);
        
        System.out.println("----------------------------------------");
        System.out.println("Is Execute allow : " + file.canExecute());
        System.out.println("Is Write allow : " + file.canWrite());
        System.out.println("Is Read allow : " + file.canRead());

    }

}
FileDemo1.java
import java.io.File;

public class FileDemo1
{

    public static void main(String[] args)
    {
        File file = new File("/home/hello.sh");

        /*
         * Marks the file or directory named by this
         * abstract pathname so that only read operations
         * are allowed.
         */
        file.setReadOnly();

        if (file.canWrite())
        {
            System.out.println("This file is writable");
        }
        else
        {
            System.out.println("This file is read only");
        }

        /*
         * A convenience method to set the owner's write
         * permission for this abstract pathname.
         */
        file.setWritable(true);

        if (file.canWrite())
        {
            System.out.println("This file is writable");
        }
        else
        {
            System.out.println("This file is read only");
        }

    }

}
Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment