How do I programmatically change file permissions?

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

    In addition to erickson's suggestions, there's also jna, which allows you to call native libraries without using jni. It's shockingly easy to use, and I've used it on a couple of projects with great success.

    The only caveat is that it's slower than jni, so if you're doing this to a very large number of files that might be an issue for you.

    (Editing to add example)

    Here's a complete jna chmod example:

    import com.sun.jna.Library;
    import com.sun.jna.Native;
    
    public class Main {
        private static CLibrary libc = (CLibrary) Native.loadLibrary("c", CLibrary.class);
    
        public static void main(String[] args) {
            libc.chmod("/path/to/file", 0755);
        }
    }
    
    interface CLibrary extends Library {
        public int chmod(String path, int mode);
    }
    

提交回复
热议问题