Thursday, 19 August 2021

DataInputStream class in Java

 

Java DataInputStream Class

Java DataInputStream class allows an application to read primitive data from the input stream in a machine-independent way.

Java application generally uses the data output stream to write data that can later be read by a data input stream.


Java DataInputStream class declaration

  1. public class DataInputStream extends FilterInputStream implements DataInput  

Java DataInputStream class Methods

MethodDescription
int read(byte[] b)It is used to read the number of bytes from the input stream.
int read(byte[] b, int off, int len)It is used to read len bytes of data from the input stream.
int readInt()It is used to read input bytes and return an int value.
byte readByte()It is used to read and return the one input byte.
char readChar()It is used to read two input bytes and returns a char value.
double readDouble()It is used to read eight input bytes and returns a double value.
boolean readBoolean()It is used to read one input byte and return true if byte is non zero, false if byte is zero.
int skipBytes(int x)It is used to skip over x bytes of data from the input stream.
String readUTF()It is used to read a string that has been encoded using the UTF-8 format.
void readFully(byte[] b)It is used to read bytes from the input stream and store them into the buffer array.
void readFully(byte[] b, int off, int len)It is used to read len bytes from the input stream.


Example

  1. package com.javawindow;  
  2. import java.io.*;    
  3. public class DataStreamExample {  
  4.   public static void main(String[] args) throws IOException {  
  5.     InputStream input = new FileInputStream("D:\\test.txt");  
  6.     DataInputStream inst = new DataInputStream(input);  
  7.     int count = input.available();  
  8.     byte[] ary = new byte[count];  
  9.     inst.read(ary);  
  10.     for (byte bt : ary) {  
  11.       char k = (char) bt;  
  12.       System.out.print(k+"-");  
  13.     }  
  14.   }  
  15. }  

Here, we are assuming that you have following data in "test.txt" file:

JAVA

Output:

J-A-V-A

DataOutputStream class in Java

 

Java DataOutputStream Class

Java DataOutputStream class allows an application to write primitive Java data types to the output stream in a machine-independent way.

Java application generally uses the data output stream to write data that can later be read by a data input stream.

Java DataOutputStream class declaration

Example:

  1. public class DataOutputStream extends FilterOutputStream implements DataOutput  

Java DataOutputStream class methods

MethodDescription
int size()It is used to return the number of bytes written to the data output stream.
void write(int b)It is used to write the specified byte to the underlying output stream.
void write(byte[] b, int off, int len)It is used to write len bytes of data to the output stream.
void writeBoolean(boolean v)It is used to write Boolean to the output stream as a 1-byte value.
void writeChar(int v)It is used to write char to the output stream as a 2-byte value.
void writeChars(String s)It is used to write string to the output stream as a sequence of characters.
void writeByte(int v)It is used to write a byte to the output stream as a 1-byte value.
void writeBytes(String s)It is used to write string to the output stream as a sequence of bytes.
void writeInt(int v)It is used to write an int to the output stream
void writeShort(int v)It is used to write a short to the output stream.
void writeShort(int v)It is used to write a short to the output stream.
void writeLong(long v)It is used to write a long to the output stream.
void writeUTF(String str)It is used to write a string to the output stream using UTF-8 encoding in portable manner.
void flush()It is used to flushes the data output stream.

Example of DataOutputStream class

In this example, we are writing the data to a text file testout.txt using DataOutputStream class.

  1. package com.javawindow;  
  2.   
  3. import java.io.*;  
  4. public class OutputExample {  
  5.     public static void main(String[] args) throws IOException {  
  6.         FileOutputStream file = new FileOutputStream(D:\\testout.txt);  
  7.         DataOutputStream data = new DataOutputStream(file);  
  8.         data.writeInt(65);  
  9.         data.flush();  
  10.         data.close();  
  11.         System.out.println("Succcess...");  
  12.     }  
  13. }  

Output:

Succcess...

testout.txt:

A

ByteArrayInputStream class in Java

 

Java ByteArrayInputStream Class

The ByteArrayInputStream is composed of two words: ByteArray and InputStream. As the name suggests, it can be used to read byte array as input stream.

Java ByteArrayInputStream class contains an internal buffer which is used to read byte array as stream. In this stream, the data is read from a byte array.

The buffer of ByteArrayInputStream automatically grows according to data.


Java ByteArrayInputStream class declaration

Example:

  1. public class ByteArrayInputStream extends InputStream  

Java ByteArrayInputStream class constructors

ConstructorDescription
ByteArrayInputStream(byte[] ary)Creates a new byte array input stream which uses ary as its buffer array.
ByteArrayInputStream(byte[] ary, int offset, int len)Creates a new byte array input stream which uses ary as its buffer array that can read up to specified len bytes of data from an array.

Java ByteArrayInputStream class methods

MethodsDescription
int available()It is used to return the number of remaining bytes that can be read from the input stream.
int read()It is used to read the next byte of data from the input stream.
int read(byte[] ary, int off, int len)It is used to read up to len bytes of data from an array of bytes in the input stream.
boolean markSupported()It is used to test the input stream for mark and reset method.
long skip(long x)It is used to skip the x bytes of input from the input stream.
void mark(int readAheadLimit)It is used to set the current marked position in the stream.
void reset()It is used to reset the buffer of a byte array.
void close()It is used for closing a ByteArrayInputStream.

Example of Java ByteArrayInputStream

Let's see a simple example of java ByteArrayInputStream class to read byte array as input stream.

  1. package com.javawindow;  
  2. import java.io.*;  
  3. public class ReadExample {  
  4.   public static void main(String[] args) throws IOException {  
  5.     byte[] buf = { 35363738 };  
  6.     // Create the new byte array input stream  
  7.     ByteArrayInputStream byt = new ByteArrayInputStream(buf);  
  8.     int k = 0;  
  9.     while ((k = byt.read()) != -1) {  
  10.       //Conversion of a byte into character  
  11.       char ch = (char) k;  
  12.       System.out.println("ASCII value of Character is:" + k + "; Special character is: " + ch);  
  13.     }  
  14.   }  
  15. }  

Output:

ASCII value of Character is:35; Special character is: #
ASCII value of Character is:36; Special character is: $
ASCII value of Character is:37; Special character is: %
ASCII value of Character is:38; Special character is: &

ByteArrayOutputStream class in Java

 

Java ByteArrayOutputStream Class

Java ByteArrayOutputStream class is used to write common data into multiple files. In this stream, the data is written into a byte array which can be written to multiple streams later.

The ByteArrayOutputStream holds a copy of data and forwards it to multiple streams.

The buffer of ByteArrayOutputStream automatically grows according to data.


Java ByteArrayOutputStream class declaration

  1. public class ByteArrayOutputStream extends OutputStream  

Java ByteArrayOutputStream class constructors

ConstructorDescription
ByteArrayOutputStream()Creates a new byte array output stream with the initial capacity of 32 bytes, though its size increases if necessary.
ByteArrayOutputStream(int size)Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

Java ByteArrayOutputStream class methods

MethodDescription
int size()It is used to returns the current size of a buffer.
byte[] toByteArray()It is used to create a newly allocated byte array.
String toString()It is used for converting the content into a string decoding bytes using a platform default character set.
String toString(String charsetName)It is used for converting the content into a string decoding bytes using a specified charsetName.
void write(int b)It is used for writing the byte specified to the byte array output stream.
void write(byte[] b, int off, int lenIt is used for writing len bytes from specified byte array starting from the offset off to the byte array output stream.
void writeTo(OutputStream out)It is used for writing the complete content of a byte array output stream to the specified output stream.
void reset()It is used to reset the count field of a byte array output stream to zero value.
void close()It is used to close the ByteArrayOutputStream.

Example of Java ByteArrayOutputStream

Let's see a simple example of java ByteArrayOutputStream class to write common data into 2 files: f1.txt and f2.txt.

  1. package com.javawindow;  
  2. import java.io.*;  
  3. public class DataStreamExample {  
  4. public static void main(String args[])throws Exception{    
  5.       FileOutputStream fout1=new FileOutputStream("D:\\f1.txt");    
  6.       FileOutputStream fout2=new FileOutputStream("D:\\f2.txt");    
  7.         
  8.       ByteArrayOutputStream bout=new ByteArrayOutputStream();    
  9.       bout.write(65);    
  10.       bout.writeTo(fout1);    
  11.       bout.writeTo(fout2);    
  12.         
  13.       bout.flush();    
  14.       bout.close();//has no effect    
  15.       System.out.println("Success...");    
  16.      }    
  17.     }   

Output:

Success...

f1.txt:

A

f2.txt:

A
Java Byte array output stream class 1

BufferedInputStream class in Java

 

Java BufferedInputStream Class

Java BufferedInputStream class is used to read information from stream. It internally uses buffer mechanism to make the performance fast.

The important points about BufferedInputStream are:

  • When the bytes from the stream are skipped or read, the internal buffer automatically refilled from the contained input stream, many bytes at a time.
  • When a BufferedInputStream is created, an internal buffer array is created.

Java BufferedInputStream class declaration

Let's see the declaration for Java.io.BufferedInputStream class:

  1. public class BufferedInputStream extends FilterInputStream  

Java BufferedInputStream class constructors

ConstructorDescription
BufferedInputStream(InputStream IS)It creates the BufferedInputStream and saves it argument, the input stream IS, for later use.
BufferedInputStream(InputStream IS, int size)It creates the BufferedInputStream with a specified buffer size and saves it argument, the input stream IS, for later use.

Java BufferedInputStream class methods

MethodDescription
int available()It returns an estimate number of bytes that can be read from the input stream without blocking by the next invocation method for the input stream.
int read()It read the next byte of data from the input stream.
int read(byte[] b, int off, int ln)It read the bytes from the specified byte-input stream into a specified byte array, starting with the given offset.
void close()It closes the input stream and releases any of the system resources associated with the stream.
void reset()It repositions the stream at a position the mark method was last called on this input stream.
void mark(int readlimit)It sees the general contract of the mark method for the input stream.
long skip(long x)It skips over and discards x bytes of data from the input stream.
boolean markSupported()It tests for the input stream to support the mark and reset methods.

Example of Java BufferedInputStream

  1. package com.javawindow;  
  2.    
  3. import java.io.*;  
  4. public class BufferedInputStreamExample{    
  5.  public static void main(String args[]){    
  6.   try{    
  7.     FileInputStream fin=new FileInputStream("D:\\test.txt");    
  8.     BufferedInputStream bin=new BufferedInputStream(fin);    
  9.     int i;    
  10.     while((i=bin.read())!=-1){    
  11.      System.out.print((char)i);    
  12.     }    
  13.     bin.close();    
  14.     fin.close();    
  15.   }catch(Exception e){System.out.println(e);}    
  16.  }    
  17. }  

Here, we are assuming that you have following data in "test.txt" file:

javaWindow

Output:

javaWindow