System.console() returns null

后端 未结 12 884
有刺的猬
有刺的猬 2020-11-22 04:55

I was using readLine of BufferedReader to get input/new password from user, but wanted to mask the password so I am trying to use java.io.Con

12条回答
  •  长发绾君心
    2020-11-22 05:19

    I also ran into this problem when trying to write a simple command line application.

    Another alternative to creating your own BufferedReader object from System.in is to use java.util.Scanner like this:

    import java.util.Scanner;
    
    Scanner in;
    in = new Scanner(System.in);
    
    String s = in.nextLine();
    

    Of course this will not be a drop-in replacement to Console, but will give you access to a variety of different input functions.

    Here's more documentation on Scanner from Oracle.

提交回复
热议问题