The Data Streams DataOutStream - RameshMF/java-io-guide GitHub Wiki

Java DataOutputStream class allows an application to write primitive Java data types to the output stream in a machine-independent way.

Java application generally uses the data output stream to write data that can later be read by a data input stream.

DataOutputStream Constructors

  • DataOutputStream(OutputStream out) - Creates a new data output stream to write data to the specified underlying output stream.

DataOutputStream Methods

  • void flush() - Flushes this data output stream.
  • int size() - Returns the current value of the counter written, the number of bytes written to this data output stream so far.
  • void write(byte[] b, int off, int len) - Writes len bytes from the specified byte array starting at offset off to the underlying output stream.
  • void write(int b) - Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.
  • void writeBoolean(boolean v) - Writes a boolean to the underlying output stream as a 1-byte value.
  • void writeByte(int v) - Writes out a byte to the underlying output stream as a 1-byte value.
  • void writeBytes(String s) - Writes out the string to the underlying output stream as a sequence of bytes.
  • void writeChar(int v) - Writes a char to the underlying output stream as a 2-byte value, high byte first.
  • void writeChars(String s) - Writes a string to the underlying output stream as a sequence of characters.
  • void writeDouble(double v) - Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first.
  • void writeFloat(float v) - Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.
  • void writeInt(int v) - Writes an int to the underlying output stream as four bytes, high byte first.
  • void writeLong(long v) - Writes a long to the underlying output stream as eight bytes, high byte first.
  • void writeShort(int v) - Writes a short to the underlying output stream as two bytes, high byte first.
  • void writeUTF(String str) - Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.

DataOutStream Example

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class to demonstrate usage of DataOutputStream class.
 * @author javaguides.net
 *
 */
public class DataOutputStreamExample {
	private static final Logger LOGGER = LoggerFactory.getLogger(DataOutputStreamExample.class);

	public static void main(String[] args) {

		final double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };
		final int[] units = { 12, 8, 13, 29, 50 };
		final String[] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" };

		try (DataOutputStream out = new DataOutputStream(
				new BufferedOutputStream(new FileOutputStream("sample.txt")))) {
			for (int i = 0; i < prices.length; i++) {
				out.writeDouble(prices[i]);
				out.writeInt(units[i]);
				out.writeUTF(descs[i]);
			}
		} catch (IOException e) {
			LOGGER.error(e.getMessage());
		}
	}
}

Reference

DataOutStream