Java中的Console類的readPassword()方法有兩種類型:
1. Java中Console類的readPassword()方法用於在禁用了回顯的情況下從控製台讀取密碼或密碼。
用法:
public char[] readPassword()
參數:此方法不接受任何參數。
返回值:此方法返回一個字符數組,其中包含從控製台讀取的密碼或口令。如果流結束,則返回null。
異常:如果發生I /O錯誤,則此方法將引發IOError。
注意:System.console()在在線IDE中返回null。
以下示例程序旨在說明IO包中Console類中的readPassword()方法:
程序1:
// Java program to illustrate
// Console readPassword() method
import java.io.*;
public class GFG {
public static void main(String[] args)
{
// Create the console object
Console cnsl
= System.console();
if (cnsl == null) {
System.out.println(
"No console available");
return;
}
// Read line
String str = cnsl.readLine(
"Enter username:");
// Print username
System.out.println(
"Username:" + str);
// Read password
// into character array
char[] ch = cnsl.readPassword(
"Enter password:");
// Print password
System.out.println(
"Password:" + ch);
}
}
程序2:
// Java program to illustrate
// Console readPassword() method
import java.io.*;
public class GFG {
public static void main(String[] args)
{
// Create the console object
Console cnsl
= System.console();
if (cnsl == null) {
System.out.println(
"No console available");
return;
}
// Read line
String str = cnsl.readLine(
"Enter username:");
// Print username
System.out.println(
"Username:" + str);
// Read password
// into character array
char[] ch = cnsl.readPassword(
"Enter password:");
// Print password
System.out.println(
"Password:" + ch);
}
}
2. Java中Console類的readPassword(String,Object)方法用於通過提供格式化的提示從控製台讀取密碼或密碼。它返回字符數組中的密碼。
用法:
public char[] readPassword(String fmt, Object... args)
參數:此方法接受兩個參數:
- fmt-它表示字符串的格式。
- args-它表示由字符串格式的格式說明符引用的參數。
返回值:此方法返回包含從控製台讀取的密碼或密碼短語的字符數組。如果流結束,則返回null。
異常:
- IllegalFormatException-如果字符串格式包含非法語法,或者格式說明符與給定參數不兼容,或者給定格式字符串或其他非法條件,則此方法將引發IllegalFormatException。
- IOError-如果發生I /O錯誤,則此方法將引發IOError。
以下示例程序旨在說明IO包中Console類中的readPassword(String,Object)方法:
程序1:
// Java program to illustrate
// Console readPassword(String, Object) method
import java.io.*;
public class GFG {
public static void main(String[] args)
{
// Create the console object
Console cnsl
= System.console();
if (cnsl == null) {
System.out.println(
"No console available");
return;
}
String fmt = "%2$5s %3$10s%n";
// Read line
String un = cnsl.readLine(
fmt, "Enter", "Username:");
// Print username
System.out.println(
"Username:" + un);
// Read password
// into character array
char[] pwd = cnsl.readPassword(
fmt, "Enter", "Password:");
// Print password
System.out.println(
"Password:" + pwd);
}
}
程序2:
// Java program to illustrate
// Console readPassword(String, Object) method
import java.io.*;
public class GFG {
public static void main(String[] args)
{
// Create the console object
Console cnsl
= System.console();
if (cnsl == null) {
System.out.println(
"No console available");
return;
}
String fmt = "%2$5s %3$10s%n";
// Read line
String un = cnsl.readLine(
fmt, "Enter", "Username:");
// Print username
System.out.println(
"Username:" + un);
// Read password
// into character array
char[] pwd = cnsl.readPassword(
fmt, "Enter", "Password:");
// Print password
System.out.println(
"Password:" + pwd);
}
}
參考文獻:
1. https://docs.oracle.com/javase/10/docs/api/java/io/Console.html#readPassword()
2. https://docs.oracle.com/javase/10/docs/api/java/io/Console.html#readPassword(java.lang.String, java.lang.Object…)
相關用法
- Java Console readLine()用法及代碼示例
- Java Console writer()用法及代碼示例
- Java Console reader()用法及代碼示例
- Java Console flush()用法及代碼示例
- Java Console format(String, Object)用法及代碼示例
- Java Console printf(String, Object)用法及代碼示例
- Java Java.io.Console用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Clock withZone()用法及代碼示例
注:本文由純淨天空篩選整理自pp_pankaj大神的英文原創作品 Console readPassword() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。