`
lengyingxin
  • 浏览: 57340 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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();


    }
    }
}
0
2
分享到:
评论
2 楼 lengyingxin 2012-09-04  
在windows下我觉得如果创建文件的同时调用setReadOnly()那我他的权限如下:is execute allow : true
is read allow : true
is write allow : false
否则他的权限就是is execute allow : true
is read allow : true
is write allow : true

file.setReadable(true)只是使文件的可读属性为true,但是setReadOnly()会决定可读,可写,可执行的权限分别为true,false,true。

在linux下使用root用户执行javac 那么创建的文件的权限不去做修改的话就应该是644,我们可以使root的可执行权限为false,但是他的可读,可写权限是不会变的。
1 楼 asialee 2012-09-03  
file.setReadable(true);和文件的setReadOnly()有什么区别。

相关推荐

Global site tag (gtag.js) - Google Analytics