<?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>Ankit Virparia &#187; Console</title>
	<atom:link href="http://ankit.co/tag/console/feed" rel="self" type="application/rss+xml" />
	<link>http://ankit.co</link>
	<description>A Programmer, Designer and Trainer</description>
	<lastBuildDate>Sun, 11 May 2014 04:15:47 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Various ways to write on Console</title>
		<link>http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-write-on-console?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=various-ways-to-write-on-console</link>
		<comments>http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-write-on-console#comments</comments>
		<pubDate>Wed, 16 Jan 2013 00:57:57 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Input Output]]></category>
		<category><![CDATA[Console]]></category>
		<category><![CDATA[IO]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=536</guid>
		<description><![CDATA[<p>When we launch an application using java.exe using command prompt at that time, cmd.exe will be our parent application. So, in this tutorial we gonna learn various techniques to send some data to parent(command prompt) application. A stream object is available with us by default which is named as: System.out where by System is the [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-write-on-console">Various ways to write on Console</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>When we launch an application using java.exe using command prompt at that time, cmd.exe will be our parent application. So, in this tutorial we gonna learn various techniques to send some data to parent(command prompt) application. A stream object is available with us by default which is named as: <strong>System.out</strong> where by System is the class of java.lang package.</p>
<p>System.out is the object of java.io.PrintStream class. And it&#8217;s capable enough to send any kind of data to console or File. So in most of the case we don&#8217;t go for any other option. But still as a part of &#8220;Various ways to write on console&#8221;, we gonna discuss several method of writing data on console/command prompt. You can see the <a href="http://docs.ankit.co/api/java/io/PrintStream.html" title="Javadoc PrintStream">list of methods here</a>.</p>
<h2>PrintStream &#8211; System.out</h2>
<pre class="brush: java; title: ; notranslate">
import java.util.*;
class SODemo
{
	public static void main(String[] s)
	{
		System.out.println(1);  // println(int)
		System.out.println(1.2f);  // println(float)
		System.out.println(1.2);  // println(double)
		System.out.println(&quot;Ankit&quot;);  // println(String)
		System.out.println('c');  // println(char)
		System.out.println(new Date());  // println(Object)
		System.out.println(new myOPClass(9));  // println(Object)	
	}
}

class myOPClass
{
	int x = 0;
	myOPClass(int x)
	{
		this.x = x;	
	}
	public String toString()
	{
		return &quot;Value of X: &quot;+x;	
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/SODemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="System out example" src="../../../images/SODemo_400.jpg" /></a></p>
<p><strong>Note: </strong>PrintStream is derived from OutputStream class of java.io package. PrintStream is a Filtered Output Stream.</p>
<hr/>
<h2>OutputStream + System.out</h2>
<p>As discussed, OutputStream is the parent class of PrintStream. So a object of PrintStream class can be referenced by OutputStream.</p>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
class OSDemo
{
	public static void main(String[] s)throws Exception
	{
		String str = &quot;I wanna print this!&quot;;
		byte[] b = str.getBytes();
		
		OutputStream os = System.out;
		os.write(b);
	}
}


</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/OSDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Output Stream example" src="../../../images/OSDemo_400.jpg" /></a></p>
<hr/>
<h2>OutputStreamWriter + System.out</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
class OSWDemo
{
	public static void main(String[] s)throws Exception
	{
		String str = &quot;I wanna print this!&quot;;
		
		OutputStream os = System.out;
		OutputStreamWriter osw = new OutputStreamWriter(os);
		
		osw.write(&quot;Ankit Virparia\n\n&quot;);
		osw.flush();
		
		osw.write(str, 2,11);
		osw.flush();
		
		
	}
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/OSWDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Output Stream Writer example" src="../../../images/OSWDemo_400.jpg" /></a></p>
<hr/>
<h2>BufferedWriter + OutputStreamWriter + System.out</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
class BufferedWriterDemo
{
	public static void main(String[] s)throws Exception
	{
		String str = &quot;I wanna print this!&quot;;
		
		OutputStream os = System.out;
		OutputStreamWriter osw = new OutputStreamWriter(os);
		BufferedWriter bw = new BufferedWriter(osw);
		
		bw.write(&quot;Ankit Virparia\n\n&quot;);
		bw.flush();
		
		bw.write(str, 2,11);
		bw.flush();		
	}
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/BufferedWriterDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="BufferedWriter example" src="../../../images/BufferedWriterDemo_400.jpg" /></a></p>
<hr/>
<h2>Console</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
class ConsoleWriteDemo
{
	public static void main(String[] s)
	{
		Console con = System.console();
		PrintWriter ps = con.writer();
		ps.print(&quot;Good Morning!&quot;);
		ps.println();
		
		con.writer().println(&quot;Hi&quot;); 
	}
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/ConsoleWriteDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Console Write  example" src="../../../images/ConsoleWriteDemo_400.jpg" /></a></p>
<hr/>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-write-on-console">Various ways to write on Console</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-write-on-console/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Various ways to read from Console</title>
		<link>http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-read-from-console?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=various-ways-to-read-from-console</link>
		<comments>http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-read-from-console#comments</comments>
		<pubDate>Tue, 15 Jan 2013 10:33:16 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Input Output]]></category>
		<category><![CDATA[Console]]></category>
		<category><![CDATA[IO]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=522</guid>
		<description><![CDATA[<p>When we launch an application using java.exe using command prompt at that time, cmd.exe will be our parent application. So, in this tutorial we gonna learn various techniques to take some data from parent(command prompt) application. A stream object is available with us by default which is named as: System.in where by System is the [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-read-from-console">Various ways to read from Console</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>When we launch an application using java.exe using command prompt at that time, cmd.exe will be our parent application. So, in this tutorial we gonna learn various techniques to take some data from parent(command prompt) application. A stream object is available with us by default which is named as: <strong>System.in</strong> where by System is the class of java.lang package.</p>
<h2>InputStream &#8211; System.in</h2>
<pre class="brush: java; title: ; notranslate">
class ISDemo
{
	public static void main(String[] s)throws Exception
	{
		byte[] b = new byte[1024];
		System.out.print(&quot;Enter Text:&quot;);
		int cnt = System.in.read(b);	
		String userString = new String(b,0,cnt);
		System.out.println(&quot;No. of chars: &quot;+cnt);
		System.out.println(&quot;String: &quot;+userString);
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/ISDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Input Stream Output" src="../../../images/ISDemo_400.jpg" /></a></p>
<hr/>
<h2>InputStreamReader + Systen.in</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
class ISRDemo
{
	public static void main(String[] s)throws Exception
	{
		InputStreamReader isr = new InputStreamReader(System.in);
		int data;
		String str = &quot;&quot;;
		char c ;
		while(true)
		{
			data = isr.read();
			c = (char)data;
			if(c=='\n')break;
			str += c;
		}
		
		System.out.println(&quot;String: &quot;+str);
	}	
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/ISRDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Input Stream Reader Output" src="../../../images/ISRDemo_400.jpg" /></a></p>
<hr/>
<h2>BufferedReader +  InputStreamReader + Systen.in</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;

class BufferedReaderDemo
{
	public static void main(String[] arg)throws Exception
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.print(&quot;Enter text:&quot;);
		String str = br.readLine();
		System.out.println(&quot;String: &quot;+str);	
	}
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/BufferedReaderDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Buffered Reader Output" src="../../../images/BufferedReaderDemo_400.jpg" /></a></p>
<hr/>
<h2>Console</h2>
<pre class="brush: java; title: ; notranslate">
import java.io.*;
class ConsoleDemo
{
	public static void main(String[] s)
	{
		Console con = System.console();
		System.out.print(&quot;Enter String:&quot;);
		String str = con.readLine();
		System.out.println(&quot;String: &quot;+str); 
	}
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/ConsoleDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Console Output" src="../../../images/ConsoleDemo_400.jpg" /></a></p>
<hr/>
<h2>Scanner</h2>
<pre class="brush: java; title: ; notranslate">
import java.util.*;

class ScannerDemo
{
	public static void main(String[] arg)
	{
		Scanner sc = new Scanner(System.in);
		System.out.print(&quot;Enter text:&quot;);
		String str = sc.nextLine();
		System.out.println(&quot;String: &quot;+str);	
	}
}
</pre>
<p><strong>Output:</strong><br />
<a class="highslide"  onclick="return hs.expand(this)" href="../../../images/ScannerDemo.jpg" rel="attachment"><img class="size-full aligncenter" alt="Scanner output" src="../../../images/ScannerDemo_400.jpg" /></a></p>
<hr/>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-read-from-console">Various ways to read from Console</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/input-output/various-ways-to-read-from-console/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
