java如何设置文件的权限

import java.io.File;
import java.io.IOException;
/×
×只能给当前用户赋予对该文件的权限,调用createNewFile()方法默认的权限是644.
×/ 
public class FilePermission 
{
    public static void main( String[] args )
    {	
    	try {
    		File file = new File("/home/test3.txt");
  	      if (file.createNewFile()){
	          System.out.println("File is created!");
 	        //Runtime.getRuntime().exec("chmod 777 /home/test3.txt"); 
                   file.setExecutable(true);//设置可执行权限
	          file.setReadable(true);//设置可读权限
	          file.setWritable(true);//设置可写权限
	          System.out.println("is execute allow : " + file.canExecute());
		      System.out.println("is read allow : " + file.canRead());
		      System.out.println("is write allow : " + file.canWrite());
	      }else{
	          System.out.println("File already exists.");
	      }
 
    	} catch (IOException e) {
	      e.printStackTrace();


    }
    }
}

你可能感兴趣的:(java)