Saturday, January 9, 2021

Java.io.DataOutputStream in Java

Oracle Java Tutorial and Material, Oracle Java Learning, Oracle Java Exam Prep

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

Constructor and Description :

◉ DataOutputStream (OutputStream out) : Creates a new data output stream to write data to the specified underlying output stream.

Important Methods:

◉ void flush() : Flushes this data output stream.

Syntax :public void flush()

           throws IOException

Overrides:

flush in class FilterOutputStream

Throws:

IOException

◉ int size () : Returns the current value of the counter written, the number of bytes written to this data output stream so far.

Syntax :public final int size()

Returns:

the value of the written field.

◉ void write (byte[] b, int off, int len) : Writes len bytes from the specified byte array starting at offset off to the underlying output stream.

Syntax :public void write(byte[] b,

         int off,

         int len)

           throws IOException

Overrides:

write in class FilterOutputStream

Parameters:

b - the data.

off - the start offset in the data.

len - the number of bytes to write.

Throws:

IOException 

◉ void write (int b) : Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.

Syntax :public void write(int b)

           throws IOException

Overrides:

write in class FilterOutputStream

Parameters:

b - the byte to be written.

Throws:

IOException

◉ void writeBoolean (boolean v) : Writes a boolean to the underlying output stream as a 1-byte value.

Syntax :public final void writeBoolean(boolean v)

                        throws IOException

Parameters:

v - a boolean value to be written.

Throws:

IOException

◉ void writeByte (int v) : Writes out a byte to the underlying output stream as a 1-byte value.

Syntax :public final void writeByte(int v)

                     throws IOException

Parameters:

v - a byte value to be written.

Throws:

IOException

◉ void writeChar (int v) : Writes a char to the underlying output stream as a 2-byte value, high byte first.

Syntax :public final void writeChar(int v)

                     throws IOException

Parameters:

v - a char value to be written.

Throws:

IOException

◉ void writeDouble (double v) : Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first.

Syntax :public final void writeDouble(double v)

                       throws IOException

Parameters:

v - a double value to be written.

Throws:

IOException

◉ void writeFloat (float v): Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, MSB first.

Syntax :public final void writeFloat(float v)

                      throws IOException

Parameters:

v - a float value to be written.

Throws:

IOException

◉ void writeInt (int v) : Writes an int to the underlying output stream as four bytes, high byte first.

Syntax :public final void writeInt(int v)

                    throws IOException

Parameters:

v - an int to be written.

Throws:

IOException

◉ void writeLong (long v) : Writes a long to the underlying output stream as eight bytes, high byte first.

Syntax :public final void writeLong(long v)

                     throws IOException

Parameters:

v - a long to be written.

Throws:

IOException

◉ void writeShort (int v) : Writes a short to the underlying output stream as two bytes, high byte first.

Syntax :public final void writeShort(int v)

                      throws IOException

Parameters:

v - a short to be written.

Throws:

IOException 

◉ void writeUTF (String str) : Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.

Syntax :public final void writeUTF(String str)

                    throws IOException

Parameters:

str - a string to be written.

Throws:

IOException

Implementations of some of the important methods

Program:

//Java program to demonstrate DataOutputStream 

// This program uses try-with-resources. It requires JDK 7 or later. 

import java.io.*; 

class DataOutputStreamDemo 

public static void main(String args[]) throws IOException 

//writing the data using DataOutputStream 

try ( DataOutputStream dout = 

new DataOutputStream(new FileOutputStream("file.dat")) ) 

dout.writeDouble(1.1); 

dout.writeInt(55); 

dout.writeBoolean(true); 

dout.writeChar('4'); 

catch(FileNotFoundException ex) 

System.out.println("Cannot Open the Output File"); 

return; 

// reading the data back using DataInputStream 

try ( DataInputStream din = 

new DataInputStream(new FileInputStream("file.dat")) ) 

double a = din.readDouble(); 

int b = din.readInt(); 

boolean c = din.readBoolean(); 

char d=din.readChar(); 

System.out.println("Values: " + a + " " + b + " " + c+" " + d); 

catch(FileNotFoundException e) 

System.out.println("Cannot Open the Input File"); 

return; 

Oracle Java Tutorial and Material, Oracle Java Learning, Oracle Java Exam Prep
Output:

Values: 1.1 55 true 4

Important Points:

◉ DataOutputStream and DataInputStream are often used together.

◉ When a DataOutputStream is closed (by calling close( )), the underlying stream specified by out is also closed automatically.

◉ There is no longer any explicit close() method call. The try-with-resources construct takes care of that.

Related Posts

0 comments:

Post a Comment