Class DataOutputStream in Java

digitization, transformation, binary

Class DataOutputStream in Java

DataOutputStream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a DataInputStream to read the data back.

DataOutputStream class is a subclass of FilterOutputStream that allows us to write Java primitive data types in a portable binary format.

Ads code goes here

We need to create a DataOutputStream by specifying the OutputStream that is to be filtered in the call to the constructor. Many of this class’s methods write a single Java primitive type, in binary format to the output stream. write() writes a single byte, an array, or a subarray of bytes. flush() forces any buffered data to be output. size() returns the number of bytes written so far.

writeUTF() outputs a Java string of Unicode characters using a slightly modified version of the UTF-8 “transformation format.” UTF-8 is an ASCII-compatible encoding of Unicode characters that are often used for the transmission and storage of Unicode text. Except for the writeUTF() method, this class is used for the binary output of data. The textual output should be done with PrintWriter or PrintStream in Java.

DataOutputStream only has methods to output primitive types. Use ObjectOutputStream to output object values.

Many of the methods of DataOutputStream write a single primitive data type, in binary format, to an underlying output stream. All multibyte quantities are written in a format that stores the most significant byte as the first byte and the least significant byte as the last byte.

The structure of class DataOutputStream is given as:

public class java.io.DataOutputStream extends java.io.FilterOutputStream implements java.io.DataOutput{
//Member element
protected int written;//the number of bytes writes to the DataOutputStream
//constructor
public DataOutputStream(OutputStream out);
//Creates a new DataOutputStream to write data to the specified output stream
//methods:
public void flush()throws IOException;
public final int size();
public synchronized void write(byte b[],int offSet,int length)throws IOException;
public synchronized void write(int b)throws IOException;
public final void writeBoolean(boolean b)throws IOException;
public final void writeByte(int v)throws IOException;
public final void writeBytes(String str)throws IOException;
public final void writeChar(int v)throws IOException;
public final void writeChars(String s)throws IOException;
public final void writeDouble(double d)throws IOException;
public final void writeFloat(float f)throws IOException;
public final void writeInt(int v)throws IOException;
public final void writeLong(long l)throws IOException;
public final void writeShort(int i)throws IOException;
public final void writeUTF(string str)throws IOException;
 

In addition,all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.
The details of the class structure are given as follows:

protected int written;

protected int written describes the number of bytes that have been written to this output stream.

public DataOutputStream(OutputStream out);

public DataOutputStream(OutputStream out) constructor creates a DataOutputStream that uses out as its underlying stream.

READ  Class URL in Java

Parameter
out – The output stream to use.

public void flush();

public void flush() method flushes the stream, forcing any buffered output to be written. The method calls the flush() method of the underlying output stream.

public final int size();

public final int size() method returns the number of bytes that have been written to the stream (i.e., it returns the value of the variable written).

This method returns the number of bytes written.

public synchronized void write(int b);

public synchronized void write(int b) method writes the low-order eight bits of b to the underlying stream as a byte.

Parameter
b – The value to write.

public synchronized void write(byte[] b, int off, int len);

public synchronized void write(byte[] b, int off, int len) method writes len bytes from the given array, starting off elements from the beginning of the array, to the underlying stream.

Parameter
b -An array of bytes to write.
off – An offset into the byte array.
len – The number of bytes to write.

public final void writeBoolean(boolean v);

public final void writeBoolean(boolean v) method writes a byte (if v is true) that contains the value 1 to the underlying stream. If v is false, the method writes a byte that contains the value 0.

Parameter
v – The boolean value to write.

public final void writeByte(int v);

public final void writeByte(int v) method writes an 8-bit byte to the underlying stream, using the low-order eight bits of the given integer v.

Parameter
v – The value to write.

public final void writeBytes(String s);

public final void writeBytes(String s) method writes the characters in the given String to the underlying stream as a sequence of 8-bit bytes. The high-order bytes of the characters in the string are ignored.

READ  Class StringWriter in Java

Parameter
s – The String to write.

public final void writeChar(int v);

public final void writeChar(int v) method writes a 16-bit char to the underlying stream, using the low-order 16 bits of the given integer v.

Parameter
v – The value to write.

public final void writeChars(String s);

public final void writeChars(String s) method writes the characters in the given String object to the underlying stream as a sequence of 16-bit characters.

Parameter
s -The String to write.

public final void writeDouble(double v);

public final void writeDouble(double v) method writes a 64-bit double to the underlying stream. The double value is converted to a long using doubleToLongBits() of Double; the long value is then written to the underlying stream as eight bytes with the high-order byte first.

Parameter
v -The double value to write.

public final void writeFloat(float v);

public final void writeFloat(float v) method writes a 32-bit float to the underlying stream. The float value is converted to a int using floatToIntBits() of Float; the int value is then written to the underlying stream as four bytes with the high-order byte first.

Parameter
v -The float value to write.

public final void writeInt(int v);

public final void writeInt(int v) method writes a 32-bit int to the underlying stream. The value is written as four bytes with the high-order byte first.

Parameter
v – The int value to write.

public final void writeLong(long v);

public final void writeLong(long v) method writes a 64-bit long to the underlying stream. The value is written as eight bytes with the high-order byte first.

Parameter
v -The long value to write.

public final void writeShort(int v);

public final void writeShort(int v) method writes a 16-bit short to the underlying stream, using the low-order two bytes of the given integer v.

READ  Class StreamCorruptedException in java

Parameter
v -The value to write.

public final void writeUTF(String str);

public final void writeUTF(String str) method writes the given String to the underlying stream using the UTF-8 encoding. First, two bytes are written as an unsigned short value; this value specifies the number of bytes to follow. The value is the actual number of bytes in the UTF-8 encoding, not the length of the string. Then each character of the string is written as UTF-8 encoded bytes.

Parameter
str – The string to write.
Apart from these DataOutputStream class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

DataOutputStream class also has inherited methods from class- FilterOutputStream. They are as follows:

  • close()
  • write(byte[])

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *