Input / Output in Java

 Java Streams are classified into two basic types:
a) Input Stream: Read data from Source and send it to program
b) Output Stream: Takes data from program and send it to destination.

Java define two types of Stream:
a) Byte Stream: Handling input/ output  of bytes, it use to read and write binary data.
b) Character Stream: Handles data in terms of Characters it use Unicode so its more efficient.

### Byte Stream Class: Reads 8 bits bytes as a stream.

1) InputStream Class: Its a abstract  class which define methods for Reading and Closing Stream.Most of the methods defined by InputStream Class is dummy. Actual body is implementing by sub-classes.
close(); //Release the source related to the stream
read();  // read next byte
read(byte[] b) ;   //Read  Some number of bytes and store it into buffer array b
read(byte[] b , int offset, int length) //

2) DataInputStream: Its an interface, Which defines methods for reading primitives from an InputStream.
boolean readBoolean()
byte      readByte()
..........................//all primitives char, float, int,long, double

3) OutStream Class: Its an abstract class it declare some methods for writing bytes, closing streams, flushing.

Methods of the OutputStream Class:

close()   // close this stream and release resource associated to this stream.
flush()   // flush the output stream and forces and buffered output bytes to written out.
write(byte[] b)  // write from b array to output stream.
write(byte[]b, int offset, int length)
write(int b);

 4) DataOutputStream: Same like DataInputStream
void writeBoolean(boolean b);

### Character Stream:
5) ReaderStream:Its like the InputStream class  only difference is that it read characters
close()
read()......

Sub-Classes are:
BufferedReader  : reading from input stream
FileReader : reading from file
InputStreamReader : Its a bridge from byte stream to character stream. it reads bytes and decodes them into characters.

6) WriterClass: Its an abstract class like Output stream
close()
flush();

Writer sub-classes:
bufferedWriter
FileWriter
OutputStreamWriter
PrintWriter

### Files:

7) File Class:
Constructors :
                      File(String dirPath)
                      File(String dirPath, String filename)
                      File(File dirObj, String filename)
File f1 = new File("/");

File Class:
canRead();
canWrite();
createNewFile();
delete();
..
8) RandomAccessFile:
seek(long newpos)   // After seek() new read /write operation occur from newpos.
getFilePointer()  //       return the position of next /write

9) FileOutputStream:
10)BufferedInputStream
11)PrintStream
12) FileWriter
13) FileReader
14) BufferedReader
15) BufferedWriter
16) PrintWriter
17) Scanner Class

boolean hasNext()
boolean hasNextBoolean()  ....// for int, float...
String next()
int nextInt() ...................// for int, float...


18) Serialization : Serialization is a process of writing object to a byte stream. Serialization also needed in RMI(Remote Method Invocation) which is use to invoke a method on different machine  sending machine serialize the object and receiving machine de-serialize  the object.
Serialization is just a marker that class can be serialize and all sub-classes of serialize class also serialize. Variables Static ans transient are not saved during serialization

a) ObjectOutput: extends DataOutput and support object serialization
void writeObject(Object object)   /// to serialize a object

b) ObjectOutputStream: extends OutputStream, its responsible to writing object  on stream.
void writeObject(Object obj); // write object to storage or stream

c)ObjectInput: extends DataInput interface
Object readObject()

Comments

Popular posts from this blog

Introduction to Java

Creational Design Pattern: Factory Design Pattern

Design Patterns: The Secret Weapon of Expert Developers