Lesson 8 | Reading 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 a File in Java which reads floating point radii
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()
DataInputStream Methods
Read Methods in the DataInputStream Class in Java SE 17
In Java SE 17, the DataInputStream class is part of the java.io package and provides methods to read primitive data types and strings from an input stream.
List of read Methods in DataInputStream
-
int read()
- Reads the next byte of data from the input stream.
- Returns: The byte read, or
-1
if the end of the stream is reached.
- Throws:
IOException
-
int read(byte[] b)
- Reads up to
b.length
bytes of data into an array.
- Returns: The number of bytes read, or
-1
if the end of the stream is reached.
- Throws:
IOException
-
int read(byte[] b, int off, int len)
- Reads up to
len
bytes into an array starting from index off
.
- Returns: The number of bytes read, or
-1
if the end of the stream is reached.
- Throws:
IOException
-
boolean readBoolean()
- Reads one byte and returns
true
if the byte is nonzero, false
otherwise.
- Throws:
IOException
-
byte readByte()
- Reads one byte and returns it as a
byte
.
- Throws:
IOException
, EOFException
-
char readChar()
- Reads two bytes and returns a
char
.
- Throws:
IOException
, EOFException
-
double readDouble()
- Reads eight bytes and returns a
double
.
- Throws:
IOException
, EOFException
-
float readFloat()
- Reads four bytes and returns a
float
.
- Throws:
IOException
, EOFException
-
void readFully(byte[] b)
- Reads
b.length
bytes into the provided array.
- Throws:
IOException
, EOFException
-
void readFully(byte[] b, int off, int len)
- Reads exactly
len
bytes into an array starting from off
.
- Throws:
IOException
, EOFException
-
int readInt()
- Reads four bytes and returns an
int
.
- Throws:
IOException
, EOFException
-
String readLine()
(Deprecated)
- Reads a line of text (terminated by
\n
, \r
, or \r\n
).
- Returns: The read line as a
String
, or null
if EOF is reached.
- Throws:
IOException
- Deprecated because it does not properly convert bytes to characters.
-
long readLong()
- Reads eight bytes and returns a
long
.
- Throws:
IOException
, EOFException
-
short readShort()
- Reads two bytes and returns a
short
.
- Throws:
IOException
, EOFException
-
int readUnsignedByte()
- Reads one byte and returns it as an
int
in the range [0, 255]
.
- Throws:
IOException
, EOFException
-
int readUnsignedShort()
- Reads two bytes and returns an
int
in the range [0, 65535]
.
- Throws:
IOException
, EOFException
-
String readUTF()
- Reads a string encoded in UTF-8.
- Returns: The UTF-8 decoded
String
.
- Throws:
IOException
, EOFException
These methods allow efficient reading of structured binary data from an input stream.
Method Signatures found in the DataInputStream class in Java SE 17 .
Review of Method Signatures
Method |
Remarks |
public final int readUnsignedByte() throws IOException |
Defined in DataInputStream , returns an unsigned byte as an int (0-255). |
public final short readShort() throws IOException |
Reads two bytes as a short . |
public final int readUnsignedShort() throws IOException |
Reads two bytes as an unsigned short , returning an int (0-65535). |
public final char readChar() throws IOException |
Reads two bytes as a char . |
public final int readInt() throws IOException |
Reads four bytes as an int . |
public final long readLong() throws IOException |
Reads eight bytes as a long . |
public final float readFloat() throws IOException |
Reads four bytes as a float . |
public final double readDouble() throws IOException |
Reads eight bytes as a double . |
public final String readUTF() throws IOException |
Reads a UTF-8 encoded string. |
public static final String readUTF(DataInput in) throws IOException |
A static method in DataInputStream , used to read a UTF-8 string from a DataInput object. |
Final Notes
- The last method (readUTF(DataInput in)) is a static utility method in DataInputStream that allows reading a UTF-8 string from any DataInput instance.
- The final keyword ensures that these methods cannot be overridden in subclasses.
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
