HOME      BLOG       FORUMS      CONTACT       ABOUT

Change file or directory permission using java

Change file or directory permission using java

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

import java.io.File;
/**
 * File class examples.
 * Performs different permission operations on a file or directory.
 */
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.

		// Make the file or directory as executable
		file.setExecutable(true);

		// Make the file or directory as read only
		file.setReadOnly();

		// Make the file as writable.
		file.setWritable(true);

		// Give read permission to the file or directory
		file.setReadable(true);
	}
}

Share

Tags: , , , ,


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply