Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Thread Tools Search this Thread
Old 30-12-2008
Join Date: Dec 2008
Posts: 28
Getting Username and Password with New java.io.Console Class

i am trying to become familiar with the Console , as i am working on java.i am trying to Getting Username and Password with console.any one can help me little to find something??
thank you.
Reply With Quote
Old 30-12-2008
Join Date: Nov 2005
Posts: 3,023
Re: Getting Username and Password with New java.io.Console Class

you loking for some interesting thing check this .
Getting a user?s username and password is probably one of the more common uses of the Console class. It?s fairly simple to do, but there are some things to look out for. Before you do anything else, you have to get a Console instance. Looking at the API, you?ll notice that Console has no public constructors. In fact, the only way to get a Console instance is by calling the System method:

Console con = System.console();

Simple enough, right? There is something you need to watch out for, however: The System.console() method can return null. According to the API:

If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

So, all you have to do is start it interactively, right? Well, sort of. This is the way it should work, but currently Eclipse (and some other IDEs) don?t yet support this feature ?properly?, so if you try to run a program that calls System.console() from within your favorite IDE, it may return null. You have been warned.

The following code demonstrates one method for getting login information.

The first thing here is getting a Console instance and checking to make sure it?s not null before proceeding. Once you have a Console instance, you can use the readLine and readPassword methods to get the login information. Both of these methods are overloaded, and include the following overloads:

The versions you use must combine prompting the user and getting input into one. If you really wanted to separate the two, you could use the Console.printf method to output your prompt and one of the no-arg methods above to get user input. That?s not necessarily incorrect, but definitely more verbose.

The readLine method takes a formatted String and a variable list of arguments; exactly like the System.out.printf method. The readLine method returns a line of input from the console, as a String, not including line termination characters. It returns null, if the end of the stream has been reached. The readPassword method is almost identical in functionality, except that it returns a char array of the user input. You?ll also notice that it disables console echo, so someone walking by can?t see what?s being typed.

Once you have the login information, using a method called authenticate to check it. The implementation of this method is outside of the scope of this post. You could stub it out and test it, using hard-coded values.

as you done using the password information, you blank it out using the Arrays.fill utility method. This will minimize the lifetime of sensitive (password) data in memory.

Incidentally, also wrapped all of this in a loop that will run until the authenticate method returns true or until count is equal to MAX_LOGINS.

The Console class also has a few other methods, but the above four are the most interesting and likely to be the most-used. You might also use either the printf or format methods. According to the spec, these methods both behave identically to each other and exactly like System.out.printf. If you want to get the PrintWriter associated with this console, you can use the writer() method. Similarly, the reader() method retrieves the Reader object. Last but not least is the flush() method, which is a must since Console implements Flushable. Nothing surprising or groundbreaking with these last few methods.
Reply With Quote

  TechArena Community > Software > Software Development
Tags: ,

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Similar Threads for: "Getting Username and Password with New java.io.Console Class"
Thread Thread Starter Forum Replies Last Post
Need help for getting the DSL username and password Road-Runner Networking & Security 4 03-12-2010 05:18 PM
Converting java class to java bean class Ucchal Software Development 6 09-08-2010 10:24 AM
How to read user input from console using Scanner class in java? Constantinee Software Development 4 06-02-2010 06:47 PM
How to use Console class to read user input in java? MABON Software Development 4 04-02-2010 10:17 PM
backup to a network drive is asking for username and password...whatusername and password? Lisa Hetherington Vista Help 7 04-02-2008 09:05 PM

All times are GMT +5.5. The time now is 07:49 PM.