How to convert an input of 3 octal numbers into CHMOD permissions into Binary?

旧时模样 提交于 2019-12-07 06:08:36

You will find it easier if you make your code much simpler. I suggest something like

public static void main(String[] args) {
    System.out.println(line12("5, 2, 6"));
}

public static String line12(String line) {
    String[] nums = line.trim().split(" *, *");
    StringBuilder sb = new StringBuilder();
    for (String s : nums) {
        if (sb.length() > 0) sb.append(" ");
        int num = Integer.parseInt(s);
        sb.append((num & 4) == 0 ? '-' : 'r');
        sb.append((num & 2) == 0 ? '-' : 'w');
        sb.append((num & 1) == 0 ? '-' : 'x');
    }
    return sb.toString();
}

prints

r-x -w- rw-
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!