Home » Java programming language

Java DataOutputStream writeBytes() Method with Example

DataOutputStream Class writeBytes() method: Here, we are going to learn about the writeBytes() method of DataOutputStream Class with its syntax and example.
Submitted by Preeti Jain, on March 30, 2020

DataOutputStream Class writeBytes() method

  • writeBytes() method is available in java.io package.
  • writeBytes() method is used to write the given string as a sequence of bytes to the basic output stream.
  • writeBytes() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • writeBytes() method may throw an exception at the time of writing the string.
    IOException: This exception may throw while getting any input/output error.

Syntax:

    public final void writeBytes(String str);

Parameter(s):

  • String str – represents the string to be written to the basic data output stream.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void writeBytes(String str) method 
// of DataOutputStream

import java.io.*;

public class WriteBytesOfDOS {
 public static void main(String[] args) throws Exception {
  ByteArrayOutputStream baos_stm = null;
  DataOutputStream dos_stm = null;

  String str = "Java World";

  try {
   // Instantiates ByteArrayOutputStream, DataOutputStream
   baos_stm = new ByteArrayOutputStream();
   dos_stm = new DataOutputStream(baos_stm);

   // By using writeBytes() method isto write
   // value in bytes from the given str
   // to the dos_stm stream
   dos_stm.writeBytes(str);

   // By using flush() method isto
   // flush output bytes are written out
   // to the basic output stream
   dos_stm.flush();

   // Display str in bytes
   System.out.println("dos_stm.writeBytes(str): " + str);

   System.out.println();

   // By using toByArray() method isto
   // convert the stream baos_stm to byte 
   // array
   byte[] by = baos_stm.toByteArray();

   // Loop to display each byte to the
   // baos_stm data till its end

   for (byte val: by) {
    // Display corresponding byte to each of the
    // given character represented in a string
    System.out.println("dos_stm.writeByte(): " + val);
   }
  } catch (Exception ex) {
   System.out.println(ex.toString());
  } finally {
   // this block is to free all necessary system 
   // resources linked with the stream
   if (baos_stm != null)
    baos_stm.close();

   if (dos_stm != null)
    dos_stm.close();
  }
 }
}

Output

dos_stm.writeBytes(str): Java World

dos_stm.writeByte(): 74
dos_stm.writeByte(): 97
dos_stm.writeByte(): 118
dos_stm.writeByte(): 97
dos_stm.writeByte(): 32
dos_stm.writeByte(): 87
dos_stm.writeByte(): 111
dos_stm.writeByte(): 114
dos_stm.writeByte(): 108
dos_stm.writeByte(): 100






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.