How do I programmatically change file permissions?

前端 未结 12 1035
深忆病人
深忆病人 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:38

    for Oralce Java 6:

    private static int chmod(String filename, int mode) {
        try {
            Class fspClass = Class.forName("java.util.prefs.FileSystemPreferences");
            Method chmodMethod = fspClass.getDeclaredMethod("chmod", String.class, Integer.TYPE);
            chmodMethod.setAccessible(true);
            return (Integer)chmodMethod.invoke(null, filename, mode);
        } catch (Throwable ex) {
            return -1;
        }
    }
    

    works under solaris/linux.

提交回复
热议问题