Java-io-console-readpassword-formatted
提供:Dev Guides
Java.io.Console.readPassword()メソッド
説明
- java.io.Console.readPassword(String fmt、Object … args)*メソッドはフォーマットされたプロンプトを提供し、エコーを無効にしてコンソールからパスワードを読み取ります。
宣言
以下は* java.io.Console.readPassword(String fmt、Object …の宣言です。 args)*メソッド-
public char[] readPassword(String fmt, Object... args)
パラメーター
- fmt -プロンプトテキストの書式文字列構文で説明されている書式文字列。
- args -フォーマット文字列のフォーマット指定子によって参照される引数。
戻り値
このメソッドは、コンソールから読み取られたパスワードを含む文字配列を返します。行終了文字は含まれません。ストリームの終わりに達した場合はnullが返されます。
例外
- IllegalFormatException -書式文字列に不正な構文、指定された引数と互換性のない書式指定子、書式文字列に指定された引数が不十分、またはその他の不正な条件が含まれる場合。
- IOError -I/Oエラーが発生した場合。
例
次の例は、java.io.Console.readPassword(String fmt、Object … args)メソッド。
package com.finddevguides;
import java.io.Console;
public class ConsoleDemo {
public static void main(String[] args) {
Console cnsl = null;
String alpha = null;
String fmt = "%2$5s %3$10s%n";
try {
//creates a console object
cnsl = System.console();
//if console is not null
if (cnsl != null) {
//read line from the user input
alpha = cnsl.readLine(fmt,"Your","Name: ");
//prints
System.out.println("Name is: " + alpha);
//read password into the char array
char[] pwd = cnsl.readPassword(fmt,"Enter","Password: ");
//prints
System.out.println("Password is: "+pwd);
}
} catch(Exception ex) {
//if any error occurs
ex.printStackTrace();
}
}
}
上記のプログラムをコンパイルして実行すると、次の結果が生成されます-
Your Name: Master Programmer
Name is: Master Programmer
Enter Password:
Password is: Good Morning