Types of Stream

InputStreamReader
An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
Method Description
int available() Returns 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.
void close() Closes this input stream and releases any system resources associated with the stream.
void mark(int) Marks the current position in this input stream.
boolean markSupported() Tests if this input stream supports the mark and reset methods.
int read() Reads the next byte of data from the input stream.
int read(byte[]) Reads some number of bytes from the input stream and stores them into the buffer array b.
int read(byte[], int, int) Reads up to len bytes of data from the input stream into an array of bytes.
void reset() Repositions this stream to the position at the time the mark method was last called on this input stream.
long skip(long) Skips over and discards n bytes of data from this input stream.

Example: -
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
InputStreamReader isr =null;
char[] cbuf = new char[5];
int i;
try {
// new input stream reader is created
fis = new FileInputStream("C:/test.txt");
isr = new InputStreamReader(fis);
// reads into the char buffer
i = isr.read(cbuf, 2, 3);
// prints the number of characters
System.out.println("Number of characters read: "+i);
// for each character in the character buffer
for(char c:cbuf)
{
// for empty character
if(((int)c)==0)
c='-';
// prints the characters
System.out.println(c);
}
} catch (Exception e) {
// print error
e.printStackTrace();
} finally {
// closes the stream and releases resources associated
if(fis!=null)
fis.close();
if(isr!=null)
isr.close();
}
}
}
Output: -
C:\java>javac BufferedReaderDemo.java
C:\java>java InputStreamReaderDemo
Number of characters read: 3
-
-
A
n
u
ByteArrayInputStream
A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream. An internal counter keeps track of the next byte to be supplied by the read method.
Closing a ByteArrayInputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.
The ByteArrayInputStream class allows a buffer in the memory to be used as an InputStream. The input source is a byte array. There are following forms of constructors to create ByteArrayInputStream objects
Sno Methods with Description
1 public int read()
This method reads the next byte of data from the InputStream. Returns an int as the next byte of data. If it is end of file then it returns -1.
2 public int read(byte[] r, int off, int len)
This method reads upto len number of bytes starting from off from the input stream into an array. Returns the total number of bytes read. If end of file -1 will be returned.
3 public int available()
Gives the number of bytes that can be read from this file input stream. Returns an int that gives the number of bytes to be read.
4 public void mark(int read)
This sets the current marked position in the stream. The parameter gives the maximum limit of bytes that can be read before the marked position becomes invalid.
5 public long skip(long n)
Skips n number of bytes from the stream. This returns the actual number of bytes skipped.

Example: -
import java.io.*;
public class ByteStreamTest {
public static void main(String args[])throws IOException {
ByteArrayOutputStream bOutput = new ByteArrayOutputStream(12);
while( bOutput.size()!= 10 ) {
// Gets the inputs from the user
bOutput.write(System.in.read());
}
byte b [] = bOutput.toByteArray();
System.out.println("Print the content");
for(int x= 0 ; x < b.length; x++) {
// printing the characters
System.out.print((char)b[x] + " ");
}
System.out.println(" ");
int c;
ByteArrayInputStream bInput = new ByteArrayInputStream(b);
System.out.println("Converting characters to Upper case " );
for(int y = 0 ; y < 1; y++ ) {
while(( c= bInput.read())!= -1) {
System.out.println(Character.toUpperCase((char)c));
}
bInput.reset();
}
}
}
Output: -
C:\java>javac ByteStreamTest.java
C:\java>java ByteStreamTest
a n u j b
Print the content
n u j b
Converting characters to Upper case
A
N
U
J
B
DataInputStream
The Java.io.DataInputStream class lets an application read primitive Java data types from an underlying input stream in a machine-independent way.Following are the important points about DataInputStream:
* An application uses a data output stream to write data that can later be read by a data input stream.
* DataInputStream is not necessarily safe for multithreaded access. Thread safety is optional and is the responsibility of users of methods in this class.
Sno Method & Description
1 int read(byte[] b)
This method reads some number of bytes from the contained input stream and stores them into the buffer array b
2 int read(byte[] b, int off, int len)
This method reads up to len bytes of data from the contained input stream into an array of bytes.
3 boolean readBoolean()
This method reads one input byte and returns true if that byte is nonzero, false if that byte is zero.
4 byte readByte()
This method reads and returns one input byte.
5 char readChar()
This method reads two input bytes and returns a char value.
6 double readDouble()
This method reads eight input bytes and returns a double value.
7 float readFloat()
This method reads four input bytes and returns a float value.
8 void readFully(byte[] b)
This method reads some bytes from an input stream and stores them into the buffer array b.
9 void readFully(byte[] b, int off, int len)
This method reads len bytes from an input stream.
10 int readInt()
This method reads four input bytes and returns an int value.
11 long readLong()
This method reads eight input bytes and returns a long value.
12 short readShort()
This method reads two input bytes and returns a short value.
13 int readUnsignedByte()
This method reads one input byte, zero-extends it to type int, and returns the result, which is therefore in the range 0 through 255.
14 int readUnsignedShort()
This method reads two input bytes and returns an int value in the range 0 through 65535.
15 String readUTF()
This method reads in a string that has been encoded using a modified UTF-8 format.
16 static String readUTF(DataInput in)
This method reads from the stream in a representation of a Unicode character string encoded in modified UTF-8 format; this string of characters is then returned as a String.
17 int skipBytes(int n)
This method makes an attempt to skip over n bytes of data from the input stream, discarding the skipped bytes.

DataInputStream and BufferedReader
The DataInputStream works with the binary data, while the BufferedReader work with character data.
All primitive data types can be handled by using the corresponding methods in DataInputStream class, while only string data can be read from BufferedReader class and they need to be parsed into the respective primitives.
DataInputStream is a part of filtered streams, while BufferedReader is not.
DataInputStream consumes less amount of memory space being it is binary stream, where as BufferedReader consumes more memory space being it is character stream.
The data to be handled is limited in DataInputStream, where as the number of characters to be handled has wide scope in BufferedReader.
FileReader,BufferedReader and FileInputStream
BufferedReaders are used for character data
Input/Output streams are used for reading binary data
BufferedReader br=new BufferedReader(new FileReader(new File(“location/name”)));
BufferedWriter br=new BufferedWriter(new FileWriter(new File(“location/name”)));
FileinputStream f=new FileInputStream(“path or file name”);
Make sure when providing path use // as / in java is assumed as wild character
FileInputStream: Read the contents of file as a stream of bytes.
DataInputStream: Works with the binary data
Is a part of the filtered Streams
Consumes less amount of memory space being it is a binary stream
Data to be handled is limited.
BufferedReader: Works with character data
Is not part of the filtered Streams.
Consumes more amount of mamory space being it is a character stream.
Number of characters to be handled has wide scope.


Free Web Hosting