Working with Streams

ObjectInputStream
The Java.io.ObjectInputStream class deserializes primitive data and objects previously written using an ObjectOutputStream.Following are the important points about BufferedInputStream:
* It is used to recover those objects previously serialized. It ensures that the types of all objects in the graph created from the stream match the classes present in the Java Virtual Machine.
* Classes are loaded as required using the standard mechanisms.
The java.io.ObjectInputStream.readObject() method read an object from the ObjectInputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are read. Default deserializing for a class can be overriden using the writeObject and readObject methods. Objects referenced by this object are read transitively so that a complete equivalent graph of objects is reconstructed by readObject.
The root object is completely restored when all of its fields and the objects it references are completely restored. At this point the object validation callbacks are executed in order based on their registered priorities. The callbacks are registered by objects (in the readObject special methods) as they are individually restored.
Exceptions are thrown for problems with the InputStream and for classes that should not be deserialized. All exceptions are fatal to the InputStream and leave it in an indeterminate state; it is up to the caller to ignore or recover the stream state.
Example: -
import java.io.*;
public class ObjectInputStreamDemo {
public static void main(String[] args) {
String s = "Hello World";
byte[] b = {'e', 'x', 'a', 'm', 'p', 'l', 'e'};
try {
// create a new file with an ObjectOutputStream
FileOutputStream out = new FileOutputStream("test.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
// write something in the file
oout.writeObject(s);
oout.writeObject(b);
oout.flush();
// create an ObjectInputStream for the file we created before
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));
// read and print an object and cast it as string
System.out.println("" + (String) ois.readObject());
// read and print an object and cast it as string
byte[] read = (byte[]) ois.readObject();
String s2 = new String(read);
System.out.println("" + s2);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Output: -
C:\java>javac ObjectInputStreamDemo.java
C:\java>java ObjectInputStreamDemo
Hello World
FilterInputStream
The Java.io.FilterInputStream class contains some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality.Following are the important points about FilterInputStream:
* The class itself simply overrides all methods of InputStream with versions that pass all requests to the contained input stream
* The Subclasses of this class may further override some of these methods and may also provide additional methods and fields.
Sno Method & Description
1 int available()
This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
2 void close()
This method closes this input stream and releases any system resources associated with the stream.
3 void mark(int readlimit)
This method marks the current position in this input stream.
4 boolean markSupported()
This method tests if this input stream supports the mark and reset methods.
5 int read()
This method reads the next byte of data from this input stream.
6 int read(byte[] b)
This method reads up to byte.length bytes of data from this input stream into an array of bytes.
7 int read(byte[] b, int off, int len)
This method reads up to len bytes of data from this input stream into an array of bytes.
8 void reset()
This method repositions this stream to the position at the time the mark method was last called on this input stream.
9 long skip(long n)
This method skips over and discards n bytes of data from this input stream.

Example:-
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FilterInputStreamDemo {
public static void main(String[] args) throws Exception {
InputStream is = null;
FilterInputStream fis = null;
int i=0;
char c;
try{
// create input streams
is = new FileInputStream("C://test.txt");
fis = new BufferedInputStream(is);
// read till the end of the stream
while((i=fis.read())!=-1)
{
// converts integer to character
c=(char)i;
// prints
System.out.println("Character read: "+c);
}
}catch(IOException e){
// if any I/O error occurs
e.printStackTrace();
}finally{
// releases any system resources associated with the stream
if(is!=null)
is.close();
if(fis!=null)
fis.close();
}
}
}
Output: -
C:\java>javac FilterInputStreamDemo.java
C:\java>java FilterInputStreamDemo
Character read: A
Character read: N
Character read: U
Character read: J
Character read:
Character read: B
Character read: H
Character read: A
Character read: R
Character read: G
Character read: A
Character read: V
Character read: A
Character read:
Character read: A
Character read: G
Character read: E
Character read:
Character read: 3
Character read: 8
LineNumberInputStream
The Java.io.LineNumberInputStream class is an input stream filter that provides the added functionality of keeping track of the current line number.A line is a sequence of bytes ending with a carriage return character ('\r'), a newline character ('\n'), or a carriage return character followed immediately by a linefeed character.
sno Method & Description
1 int available()
This method returns the number of bytes that can be read from this input stream without blocking.
2 int getLineNumber()
This method returns the current line number.
3 void mark(int readlimit)
This method marks the current position in this input stream.
4 int read()
This method reads the next byte of data from this input stream.
5 int read(byte[] b, int off, int len)
This method reads up to len bytes of data from this input stream into an array of bytes.
6 void reset()
This method repositions this stream to the position at the time the mark method was last called on this input stream.
7 void setLineNumber(int lineNumber)
This method sets the line number to the specified argument.
8 long skip(long n)
This method skips over and discards n bytes of data from this input stream.
.
Example: - .
import java.io.FileInputStream;.
import java.io.IOException;.
import java.io.LineNumberInputStream;.
class LineNumberInputStreamDemo {.
public static void main(String[] args) throws IOException {.
LineNumberInputStream lnis = null;.
FileInputStream fis =null;.
byte[] buf = new byte[5];.
int i;.
char c;.
try{.
// create new input stream.
fis = new FileInputStream("C:/test.txt");.
lnis = new LineNumberInputStream(fis);.
// read bytes to the buffer.
i=lnis.read(buf, 2, 3);.
System.out.println("The number of char read: "+i);.
// for each byte in buffer.
for(byte b:buf).
{.
// if byte is zero.
if(b==0).
c='-';.
else.
c=(char);.
// print char.
System.out.print(c);.
}.
}catch(Exception e){.
// if any error occurs.
e.printStackTrace();.
}finally{.
// closes the stream and releases any system resources.
if(fis!=null).
fis.close();.
if(lnis!=null).
lnis.close(); .
}.
}.
}.
C:\java>javac LineNumberInputStreamDemo.java
C:\java>java LineNumberInputStreamDemo
The number of char read: 3
--ANU


Free Web Hosting