Java 6 allows to read data from system console with Console class. There are several ways to do it,but I personally like this one. Here I will show example about how to read data with Console class.
It is very easy and I will not describe how it works. If you will not understand then please notify me and I will help.
In this example I will read username and password data:
import java.io.Console;public class Main{ public static void main(String[] args){ Console console = System.console(); if (console == null){ System.err.println("No console."); System.exit(1); } String name = console.readLine("Enter username:"); System.out.println(name); char[] password = console.readPassword("Enter password:"); System.out.println(password); }}Note:run this compiled application from console java -jar “path_to_project\ConsoleExample\dist\ConsoleExample.jar”
Download code from this article.
Want to have latest information from this site? Subscribe to our feed.
No related posts.

Recent Comments