📜  Java中的控制台 readPassword() 方法和示例

📅  最后修改于: 2022-05-13 01:54:24.364000             🧑  作者: Mango

Java中的控制台 readPassword() 方法和示例

Java中Console类的readPassword()方法有两种:

1. Java中Console类的readPassword()方法用于从禁用回显的控制台读取密码或密码。

句法:

public char[] readPassword()

参数:此方法不接受任何参数。

返回值:此方法返回一个字符数组,其中包含从控制台读取的密码或密码短语。如果流结束,则返回 null。

异常:如果发生 I/O 错误,此方法将抛出IOError

注意: System.console() 在在线 IDE 中返回 null。

下面的程序说明了 IO 包中 Console 类中的 readPassword() 方法:

方案一:

// 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);
    }
}
输出:

方案二:

// 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) 方法:

方案一:

// 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);
    }
}
输出:

方案二:

// 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 Java ()
2. https://docs.oracle.com/javase/10/docs/api/java Java .lang.String, Java Java…)