Data streams read and write strings,integers,floating-point numbers,and other data. The DataInputStream and DataOutputStream classes read and write the primitive Java data types (boolean,int,double,etc.) and strings in a particular,well-defined,platform-independent format. Since DataInputStream and DataOutputStream use the same formats,they’re complementary. These classes are especially useful when you need to move data between platforms that may use different native formats for integers or floating-point numbers.
In this post I am going to show how to use data streams in work.
Let’s look to table which shows all methods which we need to know:
| Type | Written by | Read by | Format |
|---|---|---|---|
| boolean | writeBoolean(boolean b) | readBoolean() | One byte,0 if false,1 if true |
| byte | writeByte(int b) | readByte() | One byte,two’s complement |
| byte array | write(byte[] data) write(byte[] data,int offset,int length) | readFully(byte[] data) readFully(byte[] data,int offset,int length) | The bytes in the order they appear in the array or subarray |
| short | writeShort(int s) | readShort() | Two bytes,two’s complement,big-endian |
| char | writeChar(int c) | readChar() | Two bytes,unsigned,big-endian |
| int | writeInt(int i) | readInt() | Four bytes,two’s complement,big-endian |
| long | writeLong(long l) | readLong() | Eight bytes,two’s complement,big-endian |
| float | writeFloat(float f) | readFloat() | Four bytes,IEEE 754,big-endian |
| double | writeDouble(double d) | readDouble() | Eight bytes,IEEE 754,big-endian |
| unsigned byte | N/A | readUnsignedByte() | One unsigned byte |
| unsigned short | N/A | readUnsignedShort() | Two bytes,big-endian,unsigned |
| String | writeBytes(String s) | N/A | The low-order byte of each char in the string from first to last |
| String | writeChars(String s) | N/A | Both bytes of each char in the string from first to last |
| String | writeUTF(String s) | readUTF() | A signed short giving the number of bytes in the encoded string,followed by a modified UTF-8 encoding of the string |
As you can see all these methods are useful for some type of data.
Now I will show example which reads and writes integer data:
import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.EOFException;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Main{ public static void main(String[] args) throws IOException{ String fileName = "integerData.dat"; writeIntData(fileName); readData(fileName); } private static void writeIntData(String fileName){ DataOutputStream dos = null; try{ dos = new DataOutputStream(new FileOutputStream(fileName)); for (int i = 1;i <= 10000;i++){dos.writeInt(i); } } catch (IOException ex){ System.err.println(ex); } finally{ try{if (dos != null){dos.close();} } catch (IOException e){ } } } private static void readData(String fileName) throws IOException{ DataInputStream din = null; try{ FileInputStream fin = new FileInputStream(fileName); din = new DataInputStream(fin); while (true){int theNumber = din.readInt();System.out.println(theNumber); } } catch (EOFException ex){ din.close(); } catch (IOException ex){ System.err.println(ex); } }}You can use data streams for other types of data the same as I did in this example.
As you can see it is very easy.
Note:file integerData.dat is located in the current working directory.
Download code from this post.
If you’d like to get the latest posts as soon as they’re published,subscribe to our feed!
No related posts.

Recent Comments