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:
Assertions in JUnit 4 and JUnit 5
Hướng dẫn sử dụng Java Generics
Write/Read cookies using HTTP and Read a file from the internet
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Configure a Spring Boot Web Application
Working with Tree Model Nodes in Jackson
Java Program to Implement Bellman-Ford Algorithm
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Java Program to Implement Bresenham Line Algorithm
Java Program to Implement CopyOnWriteArraySet API
Java Byte Array to InputStream
Interface trong Java 8 – Default method và Static method
Java Program to Generate All Possible Subsets with Exactly k Elements in Each Subset
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Java Program to Perform Matrix Multiplication
Logout in an OAuth Secured Application
Jackson – Unmarshall to Collection/Array
Java Program to Implement Sparse Array
Guide to PriorityBlockingQueue in Java
Comparing Arrays in Java
The HttpMediaTypeNotAcceptableException in Spring MVC
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Java Program to Implement Merge Sort Algorithm on Linked List
Spring Data JPA @Query
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Introduction to the Functional Web Framework in Spring 5
Giới thiệu Google Guice – Dependency injection (DI) framework
Mix plain text and HTML content in a mail
Java Program to implement Sparse Vector
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to implement Bit Matrix
OAuth2 Remember Me with Refresh Token