
- 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(int b) method
Description
The Java DataOutputStream write(int b) method writes the specified source byte to the underlying output stream. The counter written is incremented by 1 on successful invocation.
Declaration
Following is the declaration for java.io.DataOutputStream.write(int b) method −
public void write(int b)
Parameters
b − The source byte as integer.
Return Value
This method does not return any value.
Exception
IOException− If an I/O error occurs.
Example - Usage of DataOutputStream write(int b) method
The following example shows the usage of Java DataInputStream write(int b) method. We've created ByteArrayOutputStream and DataOutputStream reference. A int[] buf is initialized with some int values. A ByteArrayOutputStream object is created. Then DataOutputStream is initialized with ByteArrayOutputStream object created before. buf array is iterated to write int to stream using write() method. As next step, stream is flushed using flush() method and ByteArrayOutputStream is iterated to print the bytes written to the stream as characters. Finally we're closing all the streams.
DataOutputStreamDemo.java
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; int[] buf = {65, 66, 67, 68, 69, 70, 71}; try { // create byte array output stream baos = new ByteArrayOutputStream(); // create data output stream dos = new DataOutputStream(baos); // write to the stream from integer array for(int i: buf) { dos.write(i); } // flushes bytes to underlying output stream dos.flush(); // for each byte in the baos buffer content for(byte b:baos.toByteArray()) { // convert 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 system resources from the streams if(baos!=null) baos.close(); if(dos!=null) dos.close(); } } }
Output
Let us compile and run the above program, this will produce the following result −
ABCDEFG
Example - Usage of DataOutputStream write(int b) method
The following example shows the usage of Java DataInputStream write(int b) method. We've created ByteArrayOutputStream and DataOutputStream reference. A ByteArrayOutputStream object is created. Then DataOutputStream is initialized with ByteArrayOutputStream object created before. We're writing few values to write() method.. As next step, stream is flushed using flush() method and ByteArrayOutputStream is iterated to print the bytes written to the stream as characters. Finally we're closing all the streams.
DataOutputStreamDemo.java
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; try { // create byte array output stream baos = new ByteArrayOutputStream(); // create data output stream dos = new DataOutputStream(baos); // write some values to stream dos.write(65); dos.write(66); // flushes bytes to underlying output stream dos.flush(); // for each byte in the baos buffer content for(byte b:baos.toByteArray()) { // convert 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 system resources from the streams if(baos!=null) baos.close(); if(dos!=null) dos.close(); } } }
Output
Let us compile and run the above program, this will produce the following result −
AB
Example - Writing a Single Byte to a File
The following example shows the usage of Java DataOutputStream write(int b) 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); // Write an integer (only the lowest 8 bits will be stored) dataOutput.write(65); // ASCII value of 'A' // Close the 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
FileOutputStream creates a new file named output.dat.
DataOutputStream is wrapped around the file stream for writing data.
write(65) writes the lowest 8 bits of 65 (01000001 in binary) to the file. Since 65 is the ASCII value of 'A', the file will contain the character 'A'.
The file (output.dat) will contain: A
Closing the stream ensures that data is properly saved.
Important Notes
write(int a) only writes 1 byte (lowest 8 bits), even if a is larger.
If you write write(300), it stores only the lowest 8 bits (300 = 0b100101100 − stores 0b01100100 = 44 in decimal).
To store the full 4-byte integer, use writeInt(int a) instead.