如何使用Scanner在Java中从STDIN静默读取? [英] How to use Scanner to read silently from STDIN in Java?

查看:80
本文介绍了如何使用Scanner在Java中从STDIN静默读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个Java程序,以静默方式从STDIN读取密码.我的意思是,不将任何按下的字符输出到终端,也不将其隐藏在命令行历史记录和操作系统进程列表ps中.

I want to make a Java program that reads a Password from STDIN silently. I mean, without outputting any pressed chars to the terminal and keeping it hidden from commandline history and the operating system processlist ps.

推荐答案

java.io.Console类可能很有用:

The class java.io.Console may be useful:

System.console().readPassword();

这将从控制台读取一系列字符,而不回显任何内容. 请注意,仅当您使用真正的控制台启动Java应用程序时,它才有效.否则,System.console()返回null.

This reads a sequence of chars from the console, without echoing anything. Note that it only works when you launch your java application with a real console. Otherwise, System.console() returns null.

这篇关于如何使用Scanner在Java中从STDIN静默读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆