Java Input/Output  «Prev   Next»

Lesson 4Data streams
ObjectiveData streams use Java-defined data types

Data Input Streams

DataInputStreams and DataOutputStreams have additional methods for reading data types. That is, instead of reading and writing bytes, they can read and write the data types defined by Java.
For example, these classes have methods for reading and writing integers, floating-point numbers, characters, and boolean values.
Instances of both DataInputStream and DataOutputStream are created based on an underlying stream. The idea is that these streams are piped. An instance of DataInputStream, for example, is tied to an instance of class InputStream. The InputStream subclass reads the individual bytes; DataInputStream assembles them into Java's data types.
Figure 07.61
This diagram represents the interaction between Java's "DataInputStream" and "InputStream" classes, illustrating how data flows from a resource to user code.
Explanation of Class Interactions
  1. Your Code
    • Your application code calls the readInt() method on the DataInputStream instance.
    • This expects to read an integer value from the stream.
  2. DataInputStream
    • The DataInputStream class is a wrapper over InputStream.
    • It reads 4 bytes from the underlying InputStream and interprets them as an int.
  3. InputStream
    • The InputStream class is the lower-level stream providing the actual data.
    • DataInputStream reads from this raw byte stream.
  4. Resource
    • The InputStream ultimately gets data from a resource (e.g., a file, network socket, or other byte-based source).
    • The bytes travel up through InputStream to DataInputStream and finally get converted into an int in your code.
Summary of Data Flow
  1. Your Code → Calls readInt() on DataInputStream.
  2. DataInputStream → Requests 4 bytes from InputStream.
  3. InputStream → Reads the raw bytes from a Resource.
  4. DataInputStream → Converts those 4 bytes into an int.
  5. Your Code → Receives the final integer value.
This structure ensures that primitive data types (like `int`, `float`, `double`, etc.) can be read directly in their correct formats from an InputStream without manual byte conversion.

DataInputStream

When you create a DataInputStream, you tie it to some other subclass of class InputStream.
Here is a DataInputStream example.
It's also useful to know about two subclasses of InputStream and OutputStream that are associated with files. These are called FileInputStream and FileOutputStream. FileInputStream and FileOutputStream instances are created by specifying a file to associate with these streams. Once you create a FileInputStream instance, reading from this instance reads from the file. And of course, writing to an instance of class FileOutputStream writes to the file.
Example of DataInputStream
To create a DataInputStream instance tied to the standard input, you can write:
DataInputStream stream = new DataInputStream(System.in);

Then, you can use the DataInputStream methods, such as readInt(), which reads 4 bytes and assembles them into an int value. In Java 1.1, reading character-based data is handled by subclasses of Reader class. To read text a line at a time, the recommended method is the readLine() method of BufferedReader class. The bridge between InputStream and BufferedReader is InputStreamReader:
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();

This way you will have read the entire line entered by the user--all in one shot. (Remember, to wrap the readLine() call in a try/catch block in case it throws an IOException.)
You will see another example of BufferedReader when we discuss the net package, coming up in the next module.

Program that uses FileInputStream and InputStreamReader to read in a File

The following program will read in the text file and at location c:\\input.txt and write it out to the console.
  1. Declare an object of type FileInputStream "inputStream" that contains the location of the input file.
  2. Chain that object with InputStreamReader

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class InputStreamReaderDemo {
 public static void main(String[] args) throws IOException {
  InputStream inputStream = new FileInputStream("c:\\input.txt");
  Reader reader = new InputStreamReader(inputStream);
  int data = 0;
  try {
   data = reader.read();
  }catch (IOException e) {
    e.printStackTrace();
  }
  while (data != -1) {
  char charInput = (char) data;
  try {
   data = reader.read();
   System.out.print(charInput); // this prints numbers
  }catch (IOException e) {
   e.printStackTrace();
  }
 }
 reader.close();
 }
}

Read Using Buffered Reader - Exercise

Here is the second part of the exercise concerning reading from the standard input.
This exercise asks you to modify the program you wrote in the previous exercise to read from DataInputStream.
Read Using Buffered Reader - Exercise

Java DataStreams - Quiz

When you finish, there is a quiz to review the material presented.
Java DataStreams - Quiz

SEMrush Software