Java IO Streams - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

Java IO Streams

 

Java IO Streams (Input & output)

 

Streams is a logical connection between Java program and file/Source.


http://smarttechguides.blogspot.com/


Need Of Streams ?

Java Application can store the data in variables, objects, collections, arrays etc.

All these data stored in stack and heap memory.

stack and heap memory part of JVM.

JVM is part of RAM.

Once the application execution over, the JVM memory of the application will be vanished. because RAM is temporary memory.

 

Java application has capable to store the values for temporary purpose only, we are unable to store the data permanently.

 

Generally, if we want to store the data permanently, Programmer uses IO Streams(files) or data base to store data permanently even after termination of the program.  

 

 

IO streams are categories into two types


1) Byte stream

2) Character stream

 

http://smarttechguides.blogspot.com/

Byte Streams:

This stream which read data in the format of bytes, These Streams can handle any kind of data like Text, Audio, Video, Images etc.

 

Character Streams:

This stream which read data in the format of characters, these Streams can handle only text.

The character Streams are faster than Byte Streams.

 

a) InputStream and OutputStream are super classes for all byte streams.

 

b) Reader and Writer are the super classes for all character streams.

 

 

InputStream and Reader classes has read() method to read data from a source.

 

OutputStream and Writer classes has write() method to write data to a destination.

 

Some of the Important classes available in java.io package is

 

FileInputStream

FileOutputStream

DataInputStream

DataOutputStream

BufferedInputStream

BufferedOutputStream

ObjectInputStream

ObjectOutputStream

 

 

FileInputStream class is used to read data from a file in the form of sequence of bytes.

 

FileOutputStream class is used to write data into the target file in the sequence of bytes.


Code:

FileInputStream fis = new FileInputStream("E:/file1.txt");

FileOutputStream fos = new FileOutputStream("E:/file2.txt");

int ch;

   while ((ch = fis.read()) != -1) {

         System.out.print((char) ch);

           fos.write((char) ch);

     }

 

DataInputStream class is used to read primitive data it may be char or numbers or other than Bytes from input stream.

 

DataOutputStream class is used to write primitive data to the output stream.

 

BufferedInputStream class is maintain internal buffer memory of 8192 bytes, during the read operation in BufferedInputStream a chunk of bytes is read from the disk and stored into the internal buffer, and from the internal buffer bytes are read individually.

Hence, number of communications to the disk is reduced.

Therefore, reading bytes is faster using the buffer input stream.

 

BufferedOutputStream class is maintain internal buffer memory, during the write operation the bytes are written into the internal buffer instead of disk.

Once buffer is filled or stream closed the whole buffer data transfer into the disk.

Hence, number of communications to the disk is reduced.

This is why writing data using buffer is faster.

 

ObjectInputStream class is used to read data as Object from source file, an ObjectInputStream deserializes primitive data and an object previously written using ObjectOutputStream.

 

ObjectOutputStream class is used to write data as an Object into the target file.

 

 

 

Serialization

Serialization is process of converting objects into stream of bytes and sending them to be underlying output streams.

Using serialization we can store object state permanently in a destination, for example file or remote computer.

Serialization operation is performed by writeObject() method of ObjectOutputStream.

 

Deserialization

Deserialization is the process of converting stream of bytes into original objects.

Deserialization operation is performed by readObject() method of ObjectInputStream.

No comments:

Post a Comment