Java.io - SPLessons

Java.io DataOutputStream

Home > Lesson > Chapter 12
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io DataOutputStream

Java.io DataOutputStream

shape Introduction

The Java.io DataOutputStream is the class used for to write data into the file from output stream. The main feature of this Java.io DataOutputStream is to write data greater than bytes like int, float, double.

shape Description

Generally the output stream will take care of writing bytes. But instead of writing bytes the Java.io DataOutputStream can write primitives in java like int, long, float, so the output stream is wrap with data output stream. Here the close() method is not called explicitly the try with resources take care of closing the data output stream. The data which was written by data output stream is reads by data input stream. For closing of data output stream there is another method from java 7 onwards that is try-with-resources.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io/DataInputStream class is declared below: Public abstract class DataOutputStream extends FilterOutputStream  implements DataOutput

shape Fields

Java.io/DataOutputStream class fields are:
  • Protected OutputStream out : The field represents the current stream have to filter.
  • Protected int written : The field represents the bytes that are written to output stream.

Class Constructors

shape Table

Constructor Description
DataOutputStream(OutputStream out) The function of this constructor is to create the data output stream instance and used to write for output stream.

Class Methods

shape Table

Method Description
void write (int b) The function of this method is to write the represented byte into output stream.
void flush () The function of this method is to flush the specified output stream.
void writeBytes (String s) The function of this method is to write the content in the form of string to output stream.
void write (byte[] b, int off, int n) By using this method it writes the data up to ‘n’ bytes from the output stream to an array of bytes.
void writeFloat (Float v) The function of this method is to write the float into 4-bytes to the output stream.
void writeLong (long v ) The function of this method is to write the long into 8-bytes to the output stream.

Inherited Methods

shape Description

From the following classes, methods are inherited to data output stream class.
  • Java.io.Object
  • Java.io.FilterOutputStream

shape Examples

Usage of void write (int b) method. [c]import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; public class DataOutputStream { public static void main(String[] args) throws IOException { ByteArrayOutputStream baos = null; DataOutputStream dos = null; int[] buf = {83, 80, 76, 69, 83, 83, 79, 78, 83}; try{ // creating a new byte array output stream baos = new ByteArrayOutputStream(); // creating a new data output stream dos = new DataOutputStream(baos); // write to the stream from integer array for(int i: buf) { dos.write(i); } // To flush bytes to underlying output stream dos.flush(); // for to represent each byte in the baos buffer content for(byte b:baos.toByteArray()) { // converting byte to char char c = (char)b; // print character System.out.print(c); } }catch(Exception e) { // if any error occurs e.printStackTrace(); }finally { // releases all the system resources from this streams if(baos!=null) baos.close(); if(dos!=null) dos.close(); } } }[/c] Output [c]SPLESSONS[/c] Usage of void writeBytes (String s) method. [c]import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; public class DataOutputStream { public static void main(String[] args) throws IOException { ByteArrayOutputStream baos = null; DataOutputStream dos = null; String s = "SP LESSONS!!"; try{ // creating a new byte array output stream baos = new ByteArrayOutputStream(); // creating a new data output stream dos = new DataOutputStream(baos); // write to the output stream from the string dos.writeBytes(s); // To flush bytes to underlying output stream dos.flush(); System.out.println(s+" in bytes:"); // For to represent each byte in the buffer content for(byte b:baos.toByteArray()) { // To print byte System.out.print(b + ","); } }catch(Exception e){ // if any error occurs e.printStackTrace(); }finally{ // releases all the system resources from this streams if(baos!=null) baos.close(); if(dos!=null) dos.close(); } } }[/c] Output The result is as folloes [c] SP LESSONS!! in bytes: 83,80,32,76,69,83,83,79,78,83,33,33[/c]

Summary

shape Key Points

  • The Java.io DataOutputStream is the class used for to write data into the file from output stream.
  • The main feature of this data output stream is to write data greater than bytes like int, float, double.