
- 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 writeByte(int v) method
Description
The Java DataOutputStream writeByte(int v) method writes a byte to the underlying stream as a 1-byte value. The counter is incremented by 1 on successful execution of this method.
Declaration
Following is the declaration for java.io.DataOutputStream.writeByte(int v) method −
public final void writeByte(int v)
Parameters
v − A byte value to be written to the stream.
Return Value
This method does not return any value.
Exception
IOException − If an I/O error occurs.
Example - Usage of DataOutputStream writeByte(int v) method
The following example shows the usage of Java DataOutputStream writeByte(int v) 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 writeByte() 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.writeByte(i); } // flushes bytes to underlying output stream dos.flush(); // for each byte in the baos buffer content for(byte b:baos.toByteArray()) { // print value System.out.print(b + " "); } } 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 −
65 66 67 68 69 70 71
Example - Usage of DataOutputStream writeByte(int v) method
The following example shows the usage of Java DataOutputStream writeByte(int v) 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 writeByte() 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.writeByte(65); dos.writeByte(66); // flushes bytes to underlying output stream dos.flush(); // for each byte in the baos buffer content for(byte b:baos.toByteArray()) { // print value System.out.print(b + " "); } } 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 −
65 66
Example - Writing and Reading a Byte to/from a File
The following example shows the usage of Java DataOutputStream writeByte(int v) method.
DataOutputStreamDemo.java
package com.tutorialspoint; import java.io.DataOutputStream; import java.io.DataInputStream; import java.io.FileOutputStream; import java.io.FileInputStream; 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 different byte values dataOutput.writeByte(65); // ASCII for 'A' dataOutput.writeByte(120); // ASCII for 'x' dataOutput.writeByte(-1); // Stored as 255 (0xFF in unsigned byte representation) // Close the output stream dataOutput.close(); System.out.println("Bytes successfully written to output.dat"); // Read the bytes back FileInputStream fileInput = new FileInputStream("output.dat"); DataInputStream dataInput = new DataInputStream(fileInput); // Read and print the byte values System.out.println("First byte: " + dataInput.readByte()); // 65 ('A') System.out.println("Second byte: " + dataInput.readByte()); // 120 ('x') System.out.println("Third byte: " + dataInput.readByte()); // -1 (0xFF as signed byte) // Close the input stream dataInput.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
Bytes successfully written to output.dat First byte: 65 Second byte: 120 Third byte: -1
Explanation
Writing Byte Values
FileOutputStream creates a file (output.dat).
writeByte(65) writes 65 (0x41 in hexadecimal, which is 'A' in ASCII).
writeByte(120) writes 120 (0x78, which is 'x' in ASCII).
writeByte(-1) writes 0xFF (-1 is stored as 255 in an unsigned byte representation).
Reading Byte Values
FileInputStream opens the file.
readByte() retrieves values as signed bytes.
65 is read as 'A'.
120 is read as 'x'.
-1 (since 0xFF is -1 in signed byte representation).
Notes
writeByte() writes a single byte (8 bits) to an output stream.
values outside the range of -128 to 127 are stored modulo 256 (e.g., writeByte(300) stores 44).
readByte() returns signed values (-128 to 127).