The DataOutputStream
class in Java is a subclass of the FilterOutputStream
class that allows you to write Java primitive data types to an output stream. It provides methods for writing boolean, byte, char, short, int, long, float, and double values, as well as strings, byte arrays, and other data types.
To use the DataOutputStream
class, you first need to create an instance of it and pass an output stream to its constructor. For example:
OutputStream outputStream = new FileOutputStream("data.bin"); DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
Once you have created a DataOutputStream
object, you can use its various methods to write data to the output stream. For example, to write an integer value, you can use the writeInt
method:
dataOutputStream.writeInt(42);
This will write the integer value 42
to the output stream in four bytes. You can also write other primitive data types and strings using similar methods:
dataOutputStream.writeBoolean(true); dataOutputStream.writeChar('A'); dataOutputStream.writeUTF("Hello, world!");
When you are finished writing data to the output stream, you should close the DataOutputStream
object to ensure that any buffered data is flushed and the underlying output stream is closed:
dataOutputStream.close();
The DataOutputStream
class is often used in conjunction with the DataInputStream
class, which allows you to read the data that was written using a DataOutputStream
object.
Java DataOutputStream class declaration:
The DataOutputStream
class in Java is declared as follows:
public class DataOutputStream extends FilterOutputStream implements DataOutput
The DataOutputStream
class extends the FilterOutputStream
class, which in turn extends the OutputStream
abstract class. This means that a DataOutputStream
object is a type of output stream that can be used to write data to a destination.
Additionally, the DataOutputStream
class implements the DataOutput
interface, which defines methods for writing various Java primitive data types to an output stream. By implementing this interface, the DataOutputStream
class provides methods for writing primitive data types such as booleans, bytes, chars, shorts, ints, longs, floats, and doubles.
Overall, the DataOutputStream
class provides a convenient way to write Java primitive data types to an output stream, and is often used in combination with the DataInputStream
class to read the data back from the stream.
Java DataOutputStream class methods:
The DataOutputStream
class in Java provides several methods for writing various Java primitive data types to an output stream, as well as other useful methods. Here are some of the key methods provided by the DataOutputStream
class:
write(int b)
: Writes a byte of data to the output stream.write(byte[] b)
: Writes an array of bytes to the output stream.write(byte[] b, int off, int len)
: Writes a subarray of bytes to the output stream.writeBoolean(boolean v)
: Writes a boolean value to the output stream.writeByte(int v)
: Writes a byte value to the output stream.writeShort(int v)
: Writes a short value to the output stream.writeChar(int v)
: Writes a char value to the output stream.writeInt(int v)
: Writes an int value to the output stream.writeLong(long v)
: Writes a long value to the output stream.writeFloat(float v)
: Writes a float value to the output stream.writeDouble(double v)
: Writes a double value to the output stream.writeBytes(String s)
: Writes a string as a sequence of bytes to the output stream.writeChars(String s)
: Writes a string as a sequence of chars to the output stream.writeUTF(String s)
: Writes a string in UTF-8 format to the output stream.size()
: Returns the current size of the output stream.flush()
: Flushes the output stream and writes any buffered data to the destination.close()
: Closes the output stream and releases any resources associated with it.
These methods allow you to write different types of data to the output stream in a convenient and efficient way. You can use them to create binary files, network protocols, and other data formats that require precise control over the encoding and serialization of data.
Example of DataOutputStream class:
Here’s an example that demonstrates how to use the DataOutputStream
class in Java to write primitive data types to a binary file:
import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; public class DataOutputStreamExample { public static void main(String[] args) { String fileName = "data.bin"; try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(fileName))) { // Write some primitive data types to the binary file dos.writeBoolean(true); dos.writeByte(127); dos.writeShort(32767); dos.writeInt(2147483647); dos.writeLong(9223372036854775807L); dos.writeFloat(3.14159f); dos.writeDouble(3.141592653589793); dos.writeChars("Hello, world!"); System.out.println("Data written to file: " + fileName); } catch (IOException e) { e.printStackTrace(); } } }
In this example, we create a DataOutputStream
object that is connected to a FileOutputStream
object for writing data to a binary file named data.bin
. We then use various methods of the DataOutputStream
class to write primitive data types such as booleans, bytes, shorts, ints, longs, floats, doubles, and characters to the file.
Once we are finished writing data, we close the DataOutputStream
object using a try-with-resources block to ensure that any buffered data is flushed and the underlying output stream is closed. Finally, we print a message to indicate that the data has been written to the file.
Note that when writing strings, we use the writeChars
method instead of the writeUTF
method to write the string in a format that can be easily read by a DataInputStream
object.