Table of Contents
Lớp java.io.Console trong java được sử dụng để lấy nội dung được nhập từ giao diện console. Nó cung cấp các phương thức để đọc văn bản và mật khẩu.
Nếu bạn đọc mật khẩu bằng cách sử dụng lớp Console, nó sẽ không được hiển thị cho người dùng.
Lớp Console được gắn với hệ thống điều khiển nội bộ. Lớp Console được giới thiệu từ JDK 1.5.
1. Các phương thức của lớp Console trong java
| Method | Description |
| Reader reader() | Nó được sử dụng để lấy ra đối tượng reader kết hợp với console |
| String readLine() | Nó được sử dụng để đọc một dòng văn bản từ console. |
| String readLine(String fmt, Object… args) | Nó cung cấp một dấu nhắc định dạng sau đó đọc dòng văn bản duy nhất từ console. |
| char[] readPassword() | Nó được sử dụng để đọc mật khẩu không được hiển thị trên console. |
| char[] readPassword(String fmt, Object… args) | Nó cung cấp một dấu nhắc định dạng sau đó đọc mật khẩu không được hiển thị trên console. |
| Console format(String fmt, Object… args) | Nó được sử dụng để ghi một chuỗi định dạng cho console output stream. |
| Console printf(String format, Object… args) | Nó được sử dụng để ghi một chuỗi vào console output stream. |
| PrintWriter writer() | Nó được sử dụng để lấy ra đối tượng PrintWriter kết hợp với console. |
| void flush() | Nó được sử dụng để xả console. |
2. Ví dụ đọc chuỗi từ Console sử dụng lớp Console
package com.maixuanviet.console;
import java.io.Console;
public class TextConsoleExample {
public static void main(String args[]) {
Console c = System.console();
System.out.println("Enter your name: ");
String n = c.readLine();
System.out.println("Welcome " + n);
}
}
Kết quả thực thi chương trình trên:
Enter your name: maixuanviet.com Welcome maixuanviet.com
3. Ví dụ đọc password sử dụng lớp Console
package com.maixuanviet.console;
import java.io.Console;
public class PasswordConsoleExample {
public static void main(String args[]) {
Console c = System.console();
System.out.println("Enter password: ");
char[] ch = c.readPassword();
// convert char array into string
String pass = String.valueOf(ch);
System.out.println("Password is: " + pass);
}
}
Kết quả thực thi chương trình trên:
Enter password: <nhập 123> Password is: 123
Related posts:
Spring MVC Content Negotiation
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Implement Trie
Java Program to Implement Bucket Sort
Java Program to Implement Dijkstra’s Algorithm using Queue
Spring Cloud – Tracing Services with Zipkin
How to Get All Spring-Managed Beans?
Java Program to Implement PriorityQueue API
Java Program to Implement Graph Structured Stack
Java Timer
Java String Conversions
4 tính chất của lập trình hướng đối tượng trong Java
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
How to Return 404 with Spring WebFlux
So sánh HashMap và Hashtable trong Java
Spring MVC Tutorial
Working with Tree Model Nodes in Jackson
Spring Boot - Eureka Server
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Allow user:password in URL
Biến trong java
Disable DNS caching
RegEx for matching Date Pattern in Java
Java Program to Implement LinkedBlockingDeque API
Java Program to Implement Hash Tables with Quadratic Probing
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Guide to java.util.Formatter
Java Program to Search for an Element in a Binary Search Tree
Java – InputStream to Reader
Intersection of Two Lists in Java
Hướng dẫn Java Design Pattern – Intercepting Filter