- Java.io package classes
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package extras
- Java.io - Interfaces
- Java.io - Exceptions
- Java.io package Useful Resources
- Java.io - Discussion
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)
To Continue Learning Please Login
Login with Google