Advertisement



< Prev
Next >



DataOutputStream



OutputStream classes write data only in terms of bytes but with DataOutputStream class we can write data of primitive types such as short, char, int, float, double, boolean, to an output stream. DataOutputStream class is a filter class which is used to wrap any output stream, to write primitive data to it.




Constructor :


DataOutputStream(OutputStream is)
This constructor takes an OutputStream object in the parameters to write data to this output stream.
Example-:
FileOutputStream fos=new FileOutputStream("D:\\TextBook.txt");
DataOutputStream dos =new DataOutputStream(fos);
We have created a DataInputStream object to write data to a file D:\\TextBook.txt, pointed by FileOutputStream object reference, fos.




Point to remember


FileOutputStream is a subclass of OutputStream




Methods for writing DataOutputStream


Some methods that we generally use while working with DataOutputStream are shown in the table below-

Methods Description
flush()
This method flushes any buffered data to be written out of this output stream.

write()
This method writes one byte at a time to this output stream.

write(byte[])
This method writes a whole byte array at a time to this output stream.

writeChar()
This method writes a character(primitive data type) at a time to this output stream.

writeFloat()
This method writes a float (primitive data type) at a time to this output stream.

writeShort()
This method writes a short(primitive data type) at a time to this output stream.

writeInt()
This method writes an int(primitive data type) at a time to this output stream.

writeLong()
This method writes a long(primitive data type) at a time to this output stream.

writeBoolean()
This method writes a boolean(primitive data type) at a time to this output stream.

size()
This method returns the total number of bytes written to DataOutputStream so far.

close()
This method closes this output stream and also frees any resources connected with this output stream.




Advertisement




Program to write primitive data types to a file using DataOutputStream.


In this program, we have created a DataOutputStream object to write date of any primitive types to a file D:\\TextBook.txt, pointed by FileOutputStream object.
//Program to write primitive data types to file using  by DataOutputStream
import java.io.*;

class A
{
public static void main(String... ar)
{
try
{
FileOutputStream fos= new FileOutputStream("D:\\TextBook.txt");
DataOutputStream dos =new DataOutputStream(fos);

dos.writeInt(1985);
dos.writeDouble(245.45);
dos.writeChar('c');
dos.writeBoolean(true);
dos.writeLong(9999999999999l);
dos.writeFloat(12.15f);
System.out.println("Number of bytes written to this stream =  "+ dos.size());

dos.close();
fos.close();
}
catch(IOException e)
{
System.out.println(e);
}

}
}



Output is -:


Number of bytes written to this stream 27
After the execution of this program, a file TextBook.txt is created in the D: drive, with contents -:
Initial Size =27
245.45
c
true
9999999999999
12.15


Program Analysis





Please share this article -




< Prev
Next >
< DataInputStream
PrintStream >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement