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:
Updating your Password
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Spring Boot Configuration with Jasypt
So sánh ArrayList và LinkedList trong Java
Introduction to Spring Cloud OpenFeign
String Processing with Apache Commons Lang 3
Chương trình Java đầu tiên
Filtering and Transforming Collections in Guava
New Features in Java 8
Collect a Java Stream to an Immutable Collection
Logout in an OAuth Secured Application
How to Get the Last Element of a Stream in Java?
Converting Between a List and a Set in Java
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Cachable Static Assets with Spring MVC
CharSequence vs. String in Java
How to Iterate Over a Stream With Indices
Java Program to Perform Insertion in a BST
Converting a Stack Trace to a String in Java
A Guide to the Java LinkedList
Simplify the DAO with Spring and Java Generics
Spring Boot - Exception Handling
Spring Boot - Creating Docker Image
Java Concurrency Interview Questions and Answers
Wiring in Spring: @Autowired, @Resource and @Inject
Create Java Applet to Simulate Any Sorting Technique
Spring Boot - Admin Server
Java Program to Implement Euclid GCD Algorithm
Java Program to Perform LU Decomposition of any Matrix
Spring Boot - Thymeleaf
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Implement Find all Cross Edges in a Graph