DataInputStream
In this chapter you will learn:
- How to use DataInputStream
- Read with DataInputStream
- Use underline BufferedInputStream
- DataOutputStream and network stream
Use DataInputStream
DataInputStream
enable you to read primitive data from a stream.
It implements DataInput
interface.
DataInput
interface defines methods that
convert primitive values from a sequence of bytes.
DataInputStream
makes it easy to store binary data,
such as integers or floating-point values, in a file.
DataInputStream
extends FilterInputStream
,
which extends InputStream
, and it implements the DataInput
interface.
Here is its only constructor:
DataInputStream(InputStream inputStream)
inputStream
specifies the input stream from which data will be read.
DataInputStream
reads a sequence of bytes and convert them into values of a primitive type.
Here is a sampling of these methods:
double readDouble( ) throws IOException
boolean readBoolean( ) throws IOException
int readInt( ) throws IOException
The following code creates a DataInputStream from FileInputStream.
import java.io.DataInputStream;
import java.io.FileInputStream;
/*from j a v a 2s . c o m*/
public class MainClass {
public static void main(String args[]) {
try {
FileInputStream fis = new FileInputStream("fileName.dat");
// Create a data input stream
DataInputStream dis = new DataInputStream(fis);
// Close file input stream
fis.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Read with DataInputStream
The following program demonstrates the use of DataInputStream:
import java.io.DataInputStream;
import java.io.FileInputStream;
//from j a v a 2 s. c om
public class MainClass {
public static void main(String args[]) {
try {
FileInputStream fis = new FileInputStream("fileName.dat");
// Create a data input stream
DataInputStream dis = new DataInputStream(fis);
// Read and display data
System.out.println(dis.readBoolean());
System.out.println(dis.readByte());
System.out.println(dis.readChar());
System.out.println(dis.readDouble());
System.out.println(dis.readFloat());
System.out.println(dis.readInt());
System.out.println(dis.readLong());
System.out.println(dis.readShort());
// Close file input stream
fis.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
Use underline BufferedInputStream
In order to make the reading from DataInputStream
faster.
We can wrap stream into BufferedInputStream
.
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
//from ja v a 2 s .co m
public class Main {
public static void main(String[] args) throws IOException {
DataInputStream dis =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("temp.tmp")));
dis.close();
}
}
DataOutputStream and network stream
The following code illustrates how to read from a network stream with
DataOutputStream
. It connects to a server with Socket class and
open a stream to read.
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
//from j av a2s.c o m
public class MainClass {
public static void main(String[] args) throws Exception {
String serverName = args[0];
int port = Integer.parseInt(args[1]);
try {
Socket client = new Socket(serverName, port);
System.out.println("Just connected to " + client.getRemoteSocketAddress());
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Hello from " + client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Next chapter...
What you will learn in the next chapter:
- How to use Java BufferedInputStream to make the reading faster
- How to create BufferedInputStream from FileInputStream
- How to read byte array out of BufferedInputStream