
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - DataOutputStream write(byte[] b, int off, int len) method
Description
The Java DataOutputStream write(byte[] b, int off, int len) method writes len bytes from the specified byte array b starting at position off to the underlying output stream.
Declaration
Following is the declaration for java.io.DataOutputStream.write(byte[] b, int off, int len) method −
public void write(byte[] b, int off, int len)
Parameters
b − The source buffer.
off − The start position off.
len − The number of bytes to write to the stream.
Return Value
This method does not return any value.
Exception
IOException − If an I/O error occurs.
Example - Usage of DataOutputStream write(byte[] b, int off, int len) method
The following example shows the usage of Java DataOutputStream write(byte[] b, int off, int len) method. We've created ByteArrayOutputStream and DataOutputStream reference. A byte[] buf is initialized with some byte values. A ByteArrayOutputStream object is created. Then DataOutputStream is initialized with ByteArrayOutputStream object created before. Then a part of byte array is written to dataoutputstream. As next step, stream is flushed using flush() method and ByteArrayOutputStream is iterated to print the bytes written to the stream. Finally we're closing all the streams.
package com.tutorialspoint; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; public class DataOutputStreamDemo { public static void main(String[] args) throws IOException { ByteArrayOutputStream baos = null; DataOutputStream dos = null; byte[] buf = {87,64,72,31,90}; try { // create byte array output stream baos = new ByteArrayOutputStream(); // create data output stream dos = new DataOutputStream(baos); // write to the stream from the source buffer dos.write(buf, 2, 3); // flushes bytes to underlying output stream dos.flush(); // for each byte in the baos buffer content for(byte b:baos.toByteArray()) { System.out.println(b); } } catch(Exception e) { // if any error occurs e.printStackTrace(); } finally { // releases all system resources from the streams if(dos!=null) dos.close(); if(baos!=null) baos.close(); } } }
Output
Let us compile and run the above program, this will produce the following result −
72 31 90
Example - Usage of DataOutputStream write(byte[] b, int off, int len) method
The following example shows the usage of Java DataOutputStream write(byte[] b, int off, int len) method. We've created ByteArrayOutputStream and DataOutputStream reference. A byte[] buf is initialized with some byte values. A ByteArrayOutputStream object is created. Then DataOutputStream is initialized with ByteArrayOutputStream object created before. Then complete byte array is written to dataoutputstream by passing index as 0 and length of byte array. As next step, stream is flushed using flush() method and ByteArrayOutputStream is iterated to print the bytes written to the stream. Finally we're closing all the streams.
package com.tutorialspoint; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; public class DataOutputStreamDemo { public static void main(String[] args) throws IOException { ByteArrayOutputStream baos = null; DataOutputStream dos = null; byte[] buf = {87,64,72,31,90}; try { // create byte array output stream baos = new ByteArrayOutputStream(); // create data output stream dos = new DataOutputStream(baos); // write to the stream from the source buffer dos.write(buf, 0, buf.length); // flushes bytes to underlying output stream dos.flush(); // for each byte in the baos buffer content for(byte b:baos.toByteArray()) { System.out.println(b); } } catch(Exception e) { // if any error occurs e.printStackTrace(); } finally { // releases all system resources from the streams if(dos!=null) dos.close(); if(baos!=null) baos.close(); } } }
Output
Let us compile and run the above program, this will produce the following result −
87 64 72 31 90
Example - Writing a Subarray of Bytes to a File
The following example shows the usage of Java DataOutputStream write(byte[] b, int off, int len) method.
DataOutputStreamDemo.java
package com.tutorialspoint; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataOutputStreamDemo { public static void main(String[] args) { try { // Create a DataOutputStream to write to a file FileOutputStream fileOutput = new FileOutputStream("output.dat"); DataOutputStream dataOutput = new DataOutputStream(fileOutput); // Byte array containing ASCII values of 'Hello, World!' byte[] message = "Hello, World!".getBytes(); // Write only "World!" (start at index 7, length 6) dataOutput.write(message, 7, 6); // Close stream dataOutput.close(); System.out.println("Data successfully written to output.dat"); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
Data successfully written to output.dat
Explanation
Create a file output stream (FileOutputStream) and wrap it with DataOutputStream.
Convert a string ("Hello, World!") to a byte array using .getBytes().
-
Write only "World!" from the byte array:
Offset (7) − Starts from 'W'
Length (6) − Writes 'World!'
File (output.dat) will contain: World!
Close the stream to save data properly.
Example - Writing a Subarray of Bytes to Console
The following example shows the usage of Java DataOutputStream write(byte[] b, int off, int len) method.
DataOutputStreamDemo.java
package com.tutorialspoint; import java.io.DataOutputStream; import java.io.IOException; public class DataOutputStreamDemo { public static void main(String[] args) { try { // Create a DataOutputStream wrapping System.out DataOutputStream dataOutput = new DataOutputStream(System.out); // Byte array containing ASCII values of "Java Programming" byte[] message = "Java Programming".getBytes(); // Write only "Programming" (start at index 5, length 11) dataOutput.write(message, 5, 11); // Flush the output to ensure data is displayed immediately dataOutput.flush(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
Programming
Explanation
Wrap System.out with DataOutputStream to write directly to the console.
Convert "Java Programming" to bytes using .getBytes().
-
Write only "Programming"−
Offset (5) − Starts from 'P'
Length (11) − Writes "Programming"
Flush the output to ensure immediate console display.