Java - DataInputStream readChar() method



Description

The Java DataInputStream readChar() method reads two bytes and returns one char value.

Declaration

Following is the declaration for java.io.DataInputStream.readChar() method −

public final char readChar()

Parameters

NA

Return Value

This method returns char value read.

Exception

  • IOException − If an I/O error occurs.

  • EOFException − If the stream reaches the end.

Example 1

The following example shows the usage of Java DataInputStream readChar() method. We've created InputStream and DataInputStream references and then initialized them with ByteArrayInputStream (populated using a byte array) and DataInputStream objects. In order to initialize DataInputStream(), we requires ByteArrayInputStream object. Once objects are created, we're checking if DataInputStream has content using available() method. Then using readChar() method, we're reading two bytes as char and move the stream pointer to next byte.

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataInputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      byte[] buf = {0, 65, 0, 68, 0, 69};     
      try {
         // create new byte array input stream
         is = new ByteArrayInputStream(buf);
         
         // create data input stream
         dis = new DataInputStream(is);
         
         // readChar till the data available to read
         while( dis.available() >0) {
            System.out.println(dis.readChar());
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases any associated system files with this stream
         if(is!=null)
            is.close();
         if(dis!=null)
            dis.close();
      }   
   }
}

Output

Let us compile and run the above program, this will produce the following result −

A
D
E

Example 2

The following example shows the usage of Java DataInputStream readChar() method. We've created InputStream and DataInputStream references and then initialized them with ByteArrayInputStream (populated using a byte array) and DataInputStream objects. In order to initialize DataInputStream(), we requires ByteArrayInputStream object. Once objects are created, we're checking if DataInputStream has content using available() method. Now as a special case, we're closing the stream and then using readChar() method, we're reading two bytes as char to see if this method throws exception or not. As a result, we can see the readChar() ignores the close() method call as underlying input stream supports read after close.

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataInputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      byte[] buf = {0, 65, 0, 68, 0, 69};
      
      try {
         // create new byte array input stream
         is = new ByteArrayInputStream(buf);
         
         // create data input stream
         dis = new DataInputStream(is);
		 
         // close the stream before reading.
		 is.close();
         dis.close();
         
         // readChar till the data available to read
         while( dis.available() >0) {
            System.out.println(dis.readChar());
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases any associated system files with this stream
         if(is!=null)
            is.close();
         if(dis!=null)
            dis.close();
      }   
   }
}

Output

Let us compile and run the above program, this will produce the following result −

A
D
E

Example 3

The following example shows the usage of Java DataInputStream readChar() method. We've created InputStream and DataInputStream references and then initialized them with ByteArrayInputStream (populated using a byte array) and DataInputStream objects. In order to initialize DataInputStream(), we requires ByteArrayInputStream object. Once objects are created, we're checking if DataInputStream has content using available() method. Then using readChar() method, we're reading the two bytes as char and move the stream pointer to next byte. Now as a special case, we're not reduced one byte from byte array and then used readChar() method. As a result, we can see the readChar() throws an EOFException.

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataInputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      byte[] buf = {0, 65, 0, 68, 0};
      
      try {
         // create new byte array input stream
         is = new ByteArrayInputStream(buf);
         
         // create data input stream
         dis = new DataInputStream(is);
         
         // readChar till the data available to read
         while( dis.available() >0) {
            System.out.println(dis.readChar());
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases any associated system files with this stream
         if(is!=null)
            is.close();
         if(dis!=null)
            dis.close();
      }   
   }
}

Output

Let us compile and run the above program, this will produce the following result −

A
D
java.io.EOFException
	at java.base/java.io.DataInputStream.readChar(DataInputStream.java:370)
	at DataInputStreamDemo.main(DataInputStreamDemo.java:21)
java_files_io.htm
Advertisements