In this example we will show how to set the file permission in Java.
Source Code
package com.beginner.examples;
import java.io.*;
public class FileOperator3Example {
public static void main(String[] args )
{
try
{
File file = new File("example.txt");
if (!file.exists())
{
file.createNewFile();
}
System.out.println("Executable: " + file.canExecute());
System.out.println("Readable: " + file.canRead());
System.out.println("Writable: "+ file.canWrite());
file.setExecutable(false);
file.setReadable(false);
file.setWritable(false);
System.out.println("Executable: " + file.canExecute());
System.out.println("Readable: " + file.canRead());
System.out.println("Writable: "+ file.canWrite());
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Output:
Executable: true
Readable: true
Writable: true
Executable: false
Readable: false
Writable: false