IO Streams in Java.

In this article,IO Streams in Java is explained in detail with examples.
The IO Streams in Java/Stream is nothing but the flow of data or information from source to destination.
IO Streams In java-javaform
Ex
(Entering/writing some-data in Notepad)
Path Keyword-->CPU-->OS-->RAM-->Monitor.
Here,"Keyboard" is source and "Monitor" is the destination.
In the realtime situation, we have to send a courier from Hyderabad to Vizag.
(Starting point)                       (Ending point)
         Hyd   --------------------->       Vizag
     (Source)                                (Destination)
Here,"Hyderabad" is source and "Vizag" is the destination.
In a computer machine,we use streams using java programming in 2 ways.
  • Input Streams.
  • Output Streams.
Basically, these two streams are classified into two types.
  • Byte Streams.
  • Character Streams.
Byte Streams are used to perform reading and writing in a java program 1 Byte at a time.
Character streams are used to perform reading and writing in java program 2 bytes of information or data at a time.   (From the topic IO Streams in Java)
Input Stream.
Reading a data/information from source to destination is known as the input stream. (From topic IO Streams in Java.)
Input Stream-javaform
In general,sources are keyword;mouse;file;memory;output; of another program.
Output Stream.
Writing the data/information from source to destination is known as the output stream.
Outputstream-javaform

In general, destination are monitor;printer;file;memory;input to another program
Note
In IO Streams in Java or both Input stream and out stream destination and source are usually "Java program".
Byte-Stream-javaform

Byte Streams Input-stream classes
Java provides different classes,using which we can perform reading of data with input stream classes.These classes are available in "java.io" package.Its hierarchy is as follows,
"Input-Stream classes"
Input Stream class-javaform
InputStream Class
This class is an abstract class,it contains the following methods:
***
i)Abstract int read()
This method reads a 1 byte of data from source and returns that as int
ii) int read(byte[])
This method reads a collection of bytes of data from source and stores that in byte[] of your program.It returns int i.e,the number of bytes successfully read from the source.
iii)int read(byte[],int offset,int length)
This method reads a collection of bytes from source and stores that data in byte[] starting from offset index and length number of bytes.
It returns int i.e, Number of bytes successfully read and stored in byte[].
Keyboard: HelloWelcome
      read(byte[],4,6)
Keyboard-Javaform
iv)long skip(long)
It skips given number of bytes while reading data/information from the source.It returns long i.e, number of bytes of data successfully skipped.
v)int available()
It returns number of bytes available in source for reading without any error.
vi)mark(int)
This method allows us to put a mark at a particular position while reading a data from the source('Later this mark allows reading a data from that position).
vii)markSupported()
This method allows us to check whether mark feature is supported by source or not.
viii)void reset()
It is used to reset all the marks from the source.
ix)void close()
This method closes the source input stream opened in a program.
Note
While performing operations with these methods,we get a checked exception called as "IO Exception". (From topic IO Streams in Java.)
Ex
//program related to IO Streams in Java.
public class Stream1
{
public static void main(String args[])
{
char cc;
StringBuffer ss=new StringBuffer();
System.out.print("Enter any name:");
try
{
while((cc=(char)System.in.read())!='\n')
{
ss.append(cc);
}
}
catch(Exception e)
{
System.out.println("Given Name is:");
System.out.println(.ss);
}
}
}
Output
Enter any name:vijay
Given Name is:vijay.


Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprisessurprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required