<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CertPal &#187; console</title>
	<atom:link href="http://www.certpal.com/blogs/tag/console/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.certpal.com/blogs</link>
	<description>Technology and certifications</description>
	<lastBuildDate>Mon, 18 Jul 2011 06:48:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Java &#8211; Console</title>
		<link>http://www.certpal.com/blogs/2009/08/java-console/</link>
		<comments>http://www.certpal.com/blogs/2009/08/java-console/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 11:14:07 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[Java certifications]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[scjp]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=8</guid>
		<description><![CDATA[Sun introduced the java.io.console class to enable developers to obtain passwords / other sensitive data from the console / terminal. This article delves into the class a little.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fjava-console%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fjava-console%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>To get sensitive user input without echoing it to the 						console/terminal, the Java SDK has introduced the <strong> java.io.Console class.</strong> The Console class is quite unique 						in the way it handles its data. It uses the native encoding of the 						system instead of the using the JVM&#8217;s default encoding.</p>
<p>The Console class provides methods to access the console/terminal, if any is 							associated with the JVM.</p>
<p><strong>Using the Console class:</strong><br />
So how do you use the Console class ? Its pretty simple really. 						First get a reference to the Console class from the System class</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Console con <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">console</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It is not always possible to get a reference to the console. Some 						scenarios prevent the developer from gaining access to the 						console. This method may return null in these cases. For example a batch 							process running on a server will not expect user input and 							usually does have a console. Your code must be capable of handling such situations.</p>
<p>Consider that you have a program that requires the user to secretly type in his/her password.</p>
<p><strong> Entering sensitive data through the console:</strong><br />
<strong> </strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Console con <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">console</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> user <span style="color: #339933;">=</span> con.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Username: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> password <span style="color: #339933;">=</span> con.<span style="color: #006633;">readPassword</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Password: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you were to run this code using the eclipse IDE it would not 						work (At least in eclipse 3.2).<br />
This is because eclipse does not handle this code properly as 							yet. Run this code from the command prompt for 							now. Here is the output of the program</p>
<p><strong> Output:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">java com.<span style="color: #006633;">Tests</span>.<span style="color: #006633;">Cert</span>
Username<span style="color: #339933;">:</span> hello
Password<span style="color: #339933;">:</span></pre></div></div>

<p>Even though the output does not show a password being typed in, 						the user has typed it in on the console. The console&#8217;s 						feature allows the user to type in the password without it being 						echoed to the screen.</p>
<p>The line of code con.readLine(&#8220;Username: &#8220;); reads data from the 							console normally and stores it into a string. It also 							echoes whatever the user types onto the screen (you can see 							&#8220;hello&#8221; on the output). The line of code 							con.readPassword(&#8220;Password: &#8220;); prompts the user for a password 							but does not echo the contents of the password to the screen. 							This allows the user to enter his/her password safely using the 							Console class. To format the output to the screen the following 							code can be used.</p>
<p><strong> Formatting data:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Console con <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">console</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> user <span style="color: #339933;">=</span> con.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Welcome %s. Please login <span style="color: #000099; font-weight: bold;">\n</span>Username:&quot;</span>,<span style="color: #0000ff;">&quot;Guest&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> password <span style="color: #339933;">=</span> con.<span style="color: #006633;">readPassword</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;What is your password %s:&quot;</span>,user<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;User: &quot;</span> <span style="color: #339933;">+</span> user <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Password: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span> <span style="color: #009900;">&#40;</span>password<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Output</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">java com.<span style="color: #006633;">Tests</span>.<span style="color: #006633;">Cert</span>
Welcome Guest. <span style="color: #006633;">Please</span> login
Username<span style="color: #339933;">:</span> hello
What is your password hello<span style="color: #339933;">:</span>
User<span style="color: #339933;">:</span> hello
Password<span style="color: #339933;">:</span> bla</pre></div></div>

<p>Notice that although the password is not visible in the console 						screen, the program was able to retrieve it. Also the line of code 						con.readPassword(&#8220;What is your password %s: &#8220;,user); will format 						the output such that the username is present when asking for the 						password. %s represents the String that was passed to the method.                          Now let&#8217;s take a look at using the format() method.</p>
<p><strong>Formatting output only: </strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Console con <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">console</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
con.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Welcome %s <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #0000ff;">&quot;Mr bond&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> user <span style="color: #339933;">=</span> con.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Username: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> password <span style="color: #339933;">=</span> con.<span style="color: #006633;">readPassword</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;What is your password %s:&quot;</span>,user<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;User: &quot;</span> <span style="color: #339933;">+</span> user <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Password: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span> <span style="color: #009900;">&#40;</span>password<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong> Output:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Welcome Mr bond
Username<span style="color: #339933;">:</span> 007
What is your password 007<span style="color: #339933;">:</span>
User<span style="color: #339933;">:</span> 007
Password<span style="color: #339933;">:</span> goldeneye</pre></div></div>

<p>In this case the formatting that was present in the readLine() 						method has been transferred to the format() method. Console also 						has a printf() method that works similar to the format() method. 						This method however has been made available only for convenience. 						Finally you can make use of the reader and writer classes to read 						and write to the Console.</p>
<p><strong> Reader and Writer: </strong><br />
Note: This code throws IOException since we need read/write access to the console</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">char</span> buffer<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
Console con <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">console</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Reader</span> reader <span style="color: #339933;">=</span> con.<span style="color: #006633;">reader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> numRead <span style="color: #339933;">=</span> reader.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// #1</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span> <span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, numRead<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Writer</span> writer <span style="color: #339933;">=</span> con.<span style="color: #006633;">writer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// #2</span>
writer.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
writer.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
writer.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong> Output:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">reader <span style="color: #339933;">--</span> #a. <span style="color: #006633;">Input</span> provided by a user
reader <span style="color: #339933;">--</span> #b. <span style="color: #006633;">Written</span> by <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>
reader <span style="color: #339933;">--</span> #c. <span style="color: #006633;">Written</span> by console writer</pre></div></div>

<p>The #a reader is the input that was provided to the program. The 						program reads this data using the Reader class. It reads the data 						into a character buffer in line #1 and prints the data to the console 						. At line #2 the console&#8217;s writer is obtained by the program 						and the contents of the character buffer are sent to the console 						output again (this time using System.out). Be wary however, when you 						use the Reader and Writer classes. They throw the IOException and 						since this is a checked Exception it must be handled. The 						code above throws this exception from a method (not shown) and thus need not 						handle it.</p>
<p><a href="../../index.html">Back to home</a>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fjava-console%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fjava-console%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2009/08/java-console/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

