Using Data Streams in Java

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:

TypeWritten byRead byFormat
booleanwriteBoolean(boolean b)readBoolean()One byte,0 if false,1 if true
bytewriteByte(int b)readByte()One byte,two’s complement
byte arraywrite(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
shortwriteShort(int s)readShort()Two bytes,two’s complement,big-endian
charwriteChar(int c)readChar()Two bytes,unsigned,big-endian
intwriteInt(int i)readInt()Four bytes,two’s complement,big-endian
longwriteLong(long l)readLong()Eight bytes,two’s complement,big-endian
floatwriteFloat(float f)readFloat()Four bytes,IEEE 754,big-endian
doublewriteDouble(double d)readDouble()Eight bytes,IEEE 754,big-endian
unsigned byteN/AreadUnsignedByte()One unsigned byte
unsigned shortN/AreadUnsignedShort()Two bytes,big-endian,unsigned
StringwriteBytes(String s)N/AThe low-order byte of each char in the string from first to last
StringwriteChars(String s)N/ABoth bytes of each char in the string from first to last
StringwriteUTF(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!

Share

No related posts.

Leave a Reply

  

  

  

You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.