Java source code examples

Java source code samples. Java code examples.

Change file or directory permission only to owner using java

* * * * * 1 votes

Change file or directory permission only to owner using java

The setReadOnly, setWritable,setReadable and setExecutable methods of the file class is used for changing the permissions on a file only to owner of the file or directory.

import java.io.File;
/**
 * File class examples.
 *
 * The class sets read only, writable, executable and readable permissions
 * for a file or directory. This permissions will be applied to the owner of
 * the file or directory only. Not everyone will get the permissions.
 */
public class FileExample  {
	public static void main(String[] args){
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Given below are the different file permissions
		// operations that can be performed on java.

		// Give executable permission to a file or directory
		file.setExecutable(true, true);

		// Set read only property to a file or a directory
		file.setReadOnly();

		// Give write permission to the file
		file.setWritable(true, true);

		// Make the file or directory as readable
		file.setReadable(true, true);
	}
}

Tags: java.io

Discuss This Code