Learn how to work with Streams using Java

Files are storage spaces where information and data’s are kept. Without files we cannot do even the simplest of tasks in our day to day computing life. Every programming language have the capability to access these files in some or other way.

Accessing files means reading from a file and writing to a file. We can perform these tasks via Java programming or in other words you can store user’s data in a file and can be retrieved later by way of searching, navigation etc.

Java handles the concept of files by using Streams. Technically speaking, stream is a path traveled by data in a program. When we read information from a floppy disk, there is a path for the same with which it is able to read. Same is the case of writing. But these readings and writings are invisible to either the programmer or the user.

We can classify Streams into two types of classes, viz; InputStream and OutputStream. InputStream is used for reading information from a file while OutputStream is used to write information to a file. Both these classes are abstract meaning you cannot create an object of that class. Each of these classes have several subclasses which you have to use for reading and writing.

InputStream and OutputStream classes contains lot of constructors and methods for performing various tasks. For example, to read data from a file we use read() method while to write data a file we use write() method. You have to follow the following basic steps to perform reading and writing operations.

(1) Import java.io package.
(2) Create an object of corresponding stream class by using the appropriate constructor.
(3) Apply read() or write() method properly.
(4) Finally, close the stream by calling close() method.

We can accept keyboard input in Java using predefined stream System.in. Other than this stream, Java provides us with System.out and System.err Streams. These Streams are declared in java.lang package. The programs shown below shows you how to accept inputs via keyboard, perform mathematical calculations etc.

Accepting Inputs from keyboard

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">class</span> Keys {
  2: <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String args[]) {
  3: <span style="color: #0000ff">int</span> x = System.in.read();
  4: System.out.println(x); 
  5: } 
  6: } 

 

Performing Arithmetical calculations

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">class</span> Calc {
  2: <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String args[]) {
<p>  3: System.out.println(&quot;<span style="color: #8b0000">Enter First value</span>&quot;);</p><p>     <span style="color: #008000">// first value accepted</span>
</p>
  4: <span style="color: #0000ff">int</span> a = System.in.read();
<p>  5: System.out.println(&quot;<span style="color: #8b0000">Enter Second value</span>&quot;);</p><p>     <span style="color: #008000">// Second value accepted</span>
</p>
<p>  6: <span style="color: #0000ff">int</span> b = System.in.read();</p><p>     <span style="color: #008000">// Sum calculated by using parseInt() of Integer class</span>
</p>
  7: <span style="color: #0000ff">int</span> c = Integer.parseInt(a) + Integer.parseInt(b);
  8: System.out.println(&quot;<span style="color: #8b0000">The sum is</span>&quot; +c);
  9: } 
 10: } 

Leave a Reply

Your email address will not be published.