DataInputStream readChar() method in Java with Examples

The readChar() method of DataInputStream class in Java is used to read two input bytes and returns a char value. This method basically reads the bytes as character that are written by writechar() method of DataOutputStream class.

Syntax:

public final char readChar()
          throws IOException

Specified By: This method is specified by readChar() method of DataInput interface.

Parameters: This method does not accept any parameter.

Return value: This method returns the char value interpreted by the two bytes of input stream.



Exceptions:

  • EOFException – It throws EOFException if the input stream is ended before two bytes can be read.
  • IOException – This method throws IOException if the stream is closed or some other I/O error occurs.

Below programs illustrate readChar() method in DataInputStream class in IO package:

Program 1: Assume the existence of file “demo.txt”.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// DataInputStream readChar() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create byte array
        byte[] buf = { 71, 69, 69, 75, 83 };
  
        // Create file output stream
        FileOutputStream outputStream
            = new FileOutputStream("c:\\demo.txt");
  
        // Create data output stream
        DataOutputStream dataOutputStr
            = new DataOutputStream(outputStream);
  
        for (byte b : buf) {
            // Write character to
            // the dataOutputStream
            dataOutputStr.writeChar(b);
        }
  
        dataOutputStr.flush();
  
        // Create file input stream
        FileInputStream inputStream
            = new FileInputStream("c:\\demo.txt");
  
        // Create data input stream
        DataInputStream dataInputStr
            = new DataInputStream(inputStream);
  
        while (dataInputStr.available() > 0) {
            // Print character
            System.out.print(
                dataInputStr.readChar());
        }
    }
}

chevron_right


Output:

Program 2: Assume the existence of file “demo.txt”.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// DataInputStream readChar() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create byte array
        byte[] buf = { 71, 69, 69, 75, 83,
                       70, 79, 82, 71, 69,
                       69, 75, 83 };
        // Create file output stream
        FileOutputStream outputStream
            = new FileOutputStream("c:\\demo.txt");
  
        // Create data output stream
        DataOutputStream dataOutputStr
            = new DataOutputStream(outputStream);
  
        for (byte b : buf) {
            // Write character to
            // the dataOutputStream
            dataOutputStr.writeChar(b);
        }
  
        dataOutputStr.flush();
  
        // Create file input stream
        FileInputStream inputStream
            = new FileInputStream("c:\\demo.txt");
  
        // Create data input stream
        DataInputStream dataInputStr
            = new DataInputStream(inputStream);
  
        while (dataInputStr.available() > 0) {
            // Print character
            System.out.print(
                dataInputStr.readChar());
        }
    }
}

chevron_right


Output:

References:
https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readChar()

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.