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
Description of Relevant Features:
  • The diagram visually represents the flow of data from a resource to the user's code, specifically illustrating the role of `DataInputStream` and `InputStream` in reading and processing input.
  • Data Flow Steps:
    • Resource: The source of raw data.
    • InputStream: Acts as a low-level byte stream for reading raw bytes from the resource.
    • DataInputStream: Wraps the `InputStream` and adds functionality to read data in a structured way (e.g., integers, floats).
    • Your Code: The final layer where the data, now processed and structured, is utilized.

Key Features: Key Features:
  • DataInputStream bridges the gap between byte-oriented InputStream and structured data types like int.
  • The read int action in DataInputStream translates four bytes of data from the InputStream into a structured integer value.
  • The diagram clearly emphasizes the layered architecture, where each layer adds more abstraction to the raw data.
This visual is helpful for understanding how Java handles input streams, particularly in scenarios requiring structured data extraction from raw input.

DataInputStream

When you create a DataInputStream, you tie it to some other subclass of class InputStream.
Here is aDataInputStream 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