Filter Streams   «Prev  Next»

Lesson 8Reading and writing data in a portable format
Objective Write a program that reads a file of floating point radii and writes a file of floating point circumferences.

Read file floating point radii

Reading and writing Data in a Portable Format

The java.io.DataInputStream and java.io.DataOutputStream classes read and write primitive Java data types and strings in a specified machine-independent format. Generally you use a DataInputStream to read data written by a DataOutputStream. By using data streams, you guarantee that the data you write on one system can be read by Java programs on other systems.
There are twelve write methods in the DataOutputStream class.

DataOutputStream Methods and Exceptions

The DataOutputStream has these methods:
public DataOutputStream(OutputStream out)
 
public synchronized void write(int b) throws IOException
 
public synchronized void write (byte b[], int offset, int length) throws IOException
 
public void flush() throws IOException
 
public final void writeBoolean(boolean v) throws IOException
 
public final void writeByte(int v) throws IOException
 
public final void writeShort(int v) throws IOException
 
public final void writeChar(int v) throws IOException


public final void writeInt(int v) throws IOException
 
public final void writeFloat(float v) throws IOException
 
public final void writeDouble(double v) throws IOException
 
public final void writeBytes(String s) throws IOException
 
public final void writeChars(String s) throws IOException
 
public final void writeUTF(String str) throws IOException
 
public final int size()
Twelve corresponding read methods in the DataInputStream Class.

DataInputStream Methods

The DataInputStream has these methods:
public DataInputStream(InputStream in)
 
public final int read(byte b[]) throws IOException
  
public final int read(byte b[], int offset, int length) throws IOException
  
public final void readFully(byte b[]) throws IOException
  
public final void readFully (byte b[], int offset, int length) throws IOException
  
public final int skipBytes(int n) throws IOException
 
public final boolean readBoolean() throws IOException
  
public final byte readByte() throws IOException

What are the most important methods in the DataInputStream Class in Java SE 5?

The most important methods in the DataInputStream class in Java SE 5 include:
  1. read() - reads a byte of data from the input stream.
  2. read(byte[] b) - reads bytes from the input stream and stores them in a byte array.
  3. readBoolean() - reads a boolean value from the input stream.
  4. readByte() - reads a byte of data from the input stream.
  5. readChar() - reads a character from the input stream.
  6. readDouble() - reads a double-precision floating-point number from the input stream.
  7. readFloat() - reads a single-precision floating-point number from the input stream.
  8. readFully(byte[] b) - reads bytes from the input stream and stores them in a byte array.
  9. readInt() - reads a 32-bit integer from the input stream.
  10. readLong() - reads a 64-bit long integer from the input stream.

 
public final int readUnsignedByte() throws IOException
  
public final short readShort() throws IOException
  
public final int readUnsignedShort() throws IOException
  
public final char readChar() throws IOException
  
public final int readInt() throws IOException
  
public final long readLong() throws IOException
  
public final float readFloat() throws IOException
  
public final double readDouble() throws IOException
  
public final String readUTF() throws IOException
  
public static final String readUTF(DataInput in) throws IOException

Matching methods

You will notice that every write method in the DataOutputStream class is matched by exactly one corresponding read method in the DataInputStream class. For writeByte(), there is readByte(). For writeShort(), there is readShort(); and so on.
The reverse is not true. The DataInputStream class also has several methods to read data commonly written by non-Java programs such as readUnsignedByte() and readUnsignedShort().

The readLine( ) method

The DataInputStream class has a readLine() method that was used quite commonly in Java 1.0. However it is deprecated in favor of BufferedReader in Java 1.1.

public final String readLine() throws IOException

The readfully( ) methods

The DataInputStream class also has useful readFully() methods that read repeatedly from the underlying stream until the byte array is filled. This contrasts with the regular read() methods, which only read as many bytes as are available at the moment.

Portable Formats - Exercise

Click the Exercise link below to write a program that reads a file of floating point radii and writes a file of floating point circumferences.
Portable Formats - Exercise