How do I programmatically change file permissions?

前端 未结 12 1040
深忆病人
深忆病人 2020-11-22 04:55

In Java, I\'m dynamically creating a set of files and I\'d like to change the file permissions on these files on a linux/unix file system. I\'d like to be able to execute t

12条回答
  •  抹茶落季
    2020-11-22 05:53

    Just to update this answer unless anyone comes across this later, since JDK 6 you can use

    File file = new File('/directory/to/file');
    file.setWritable(boolean);
    file.setReadable(boolean);
    file.setExecutable(boolean);
    

    you can find the documentation on Oracle File(Java Platform SE 7). Bear in mind that these commands only work if the current working user has ownership or write access to that file. I am aware that OP wanted chmod type access for more intricate user configuration. these will set the option across the board for all users.

提交回复
热议问题