终端输入密码不想显示

当我们在终端输入密码的时候,难免不想让这个密码显示出来,我们可以利用console方法来进行操作

    public static void main(String[] args) {
        Console input = System.console();
        String name = input.readLine("用户名字 : ");
        char[] psd = input.readPassword("Password : ");
        System.out.println(name);
        System.out.println(new String(psd));
    }

观看这个readPassword的源码可以知道这个函数的返回值类型是char[]类型,因此我们要用字符数组进行接受数据

posted @ 2023-08-16 17:23  TranquilGlow  阅读(27)  评论(0)    收藏  举报