LoginSignup
2
0

More than 5 years have passed since last update.

JavaでLinuxパーミッション

Posted at

概要

私の作っているソフトウェアで、Javaでパーミッション変更をコーディングしないといけなくなったので、書いた。

permission.listの各行に書いてある、ファイルの権限を変更する。

コード

    static ArrayList<String> permpaths;

    public static void readPermissionList(){
        permpaths = new ArrayList<>();
        Path p = Paths.get("permission.list");
        if(Files.exists(p)){
            try {
            FileReader fileReader = new FileReader(p.toFile());
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String data;
            while ((data = bufferedReader.readLine()) != null) {
                System.out.println(data);
                permpaths.add(data);
            }
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            permpaths.add("pocketmine.yml");
            permpaths.add("server.properties");
            permpaths.add("plugins/ChatPermitter.phar");
        }

    }

    public static void setPermission(){
        for(String filepath :permpaths) {
            try {
                Files.setPosixFilePermissions(Paths.get(filepath),
                        EnumSet.of(PosixFilePermission.OWNER_READ,
                                PosixFilePermission.OWNER_WRITE,
                                PosixFilePermission.OWNER_EXECUTE
                        ));
            } catch (IOException e) {
                System.out.println("error");
            }

レファレンス

https://stackoverflow.com/questions/48945147/how-to-change-permissions-to-incoming-file-in-linux-using-java
http://kojiumeda.blogspot.com/2016/10/fileattribute.html
http://kagamihoge.hatenablog.com/entry/20130110/1357810886

おすすめ文献

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0