Reading data from console with Console class in Java

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.

Share

No related posts.

Leave a Reply

  

  

  

You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.