java如何设置文件的权限

2024-11-08 16:42:09
推荐回答(1个)
回答1:

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.");}