java.io
Class DataOutputStream

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.io.OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream 
      extended by java.io.FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream 
          extended by java.io.DataOutputStream
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable , DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput , Flushable sample code for java.io.Flushable definition code for java.io.Flushable

public class DataOutputStream
extends FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream
implements DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput

A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.

Since:
JDK1.0
See Also:
DataInputStream sample code for java.io.DataInputStream definition code for java.io.DataInputStream

Field Summary
protected  int written sample code for java.io.DataOutputStream.written definition code for java.io.DataOutputStream.written
          The number of bytes written to the data output stream so far.
 
Fields inherited from class java.io.FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream
out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out
 
Constructor Summary
DataOutputStream sample code for java.io.DataOutputStream.DataOutputStream(java.io.OutputStream) definition code for java.io.DataOutputStream.DataOutputStream(java.io.OutputStream) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
          Creates a new data output stream to write data to the specified underlying output stream.
 
Method Summary
 void flush sample code for java.io.DataOutputStream.flush() definition code for java.io.DataOutputStream.flush() ()
          Flushes this data output stream.
 int size sample code for java.io.DataOutputStream.size() definition code for java.io.DataOutputStream.size() ()
          Returns the current value of the counter written, the number of bytes written to this data output stream so far.
 void write sample code for java.io.DataOutputStream.write(byte[], int, int) definition code for java.io.DataOutputStream.write(byte[], int, int) (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 sample code for java.io.DataOutputStream.write(int) definition code for java.io.DataOutputStream.write(int) (int b)
          Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.
 void writeBoolean sample code for java.io.DataOutputStream.writeBoolean(boolean) definition code for java.io.DataOutputStream.writeBoolean(boolean) (boolean v)
          Writes a boolean to the underlying output stream as a 1-byte value.
 void writeByte sample code for java.io.DataOutputStream.writeByte(int) definition code for java.io.DataOutputStream.writeByte(int) (int v)
          Writes out a byte to the underlying output stream as a 1-byte value.
 void writeBytes sample code for java.io.DataOutputStream.writeBytes(java.lang.String) definition code for java.io.DataOutputStream.writeBytes(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  s)
          Writes out the string to the underlying output stream as a sequence of bytes.
 void writeChar sample code for java.io.DataOutputStream.writeChar(int) definition code for java.io.DataOutputStream.writeChar(int) (int v)
          Writes a char to the underlying output stream as a 2-byte value, high byte first.
 void writeChars sample code for java.io.DataOutputStream.writeChars(java.lang.String) definition code for java.io.DataOutputStream.writeChars(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  s)
          Writes a string to the underlying output stream as a sequence of characters.
 void writeDouble sample code for java.io.DataOutputStream.writeDouble(double) definition code for java.io.DataOutputStream.writeDouble(double) (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 sample code for java.io.DataOutputStream.writeFloat(float) definition code for java.io.DataOutputStream.writeFloat(float) (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 sample code for java.io.DataOutputStream.writeInt(int) definition code for java.io.DataOutputStream.writeInt(int) (int v)
          Writes an int to the underlying output stream as four bytes, high byte first.
 void writeLong sample code for java.io.DataOutputStream.writeLong(long) definition code for java.io.DataOutputStream.writeLong(long) (long v)
          Writes a long to the underlying output stream as eight bytes, high byte first.
 void writeShort sample code for java.io.DataOutputStream.writeShort(int) definition code for java.io.DataOutputStream.writeShort(int) (int v)
          Writes a short to the underlying output stream as two bytes, high byte first.
 void writeUTF sample code for java.io.DataOutputStream.writeUTF(java.lang.String) definition code for java.io.DataOutputStream.writeUTF(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  str)
          Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.
 
Methods inherited from class java.io.FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream
close sample code for java.io.FilterOutputStream.close() definition code for java.io.FilterOutputStream.close() , write sample code for java.io.FilterOutputStream.write(byte[]) definition code for java.io.FilterOutputStream.write(byte[])
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 
Methods inherited from interface java.io.DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
write sample code for java.io.DataOutput.write(byte[]) definition code for java.io.DataOutput.write(byte[])
 

Field Detail

written sample code for java.io.DataOutputStream.written

protected int written
The number of bytes written to the data output stream so far. If this counter overflows, it will be wrapped to Integer.MAX_VALUE.

Constructor Detail

DataOutputStream sample code for java.io.DataOutputStream(java.io.OutputStream) definition code for java.io.DataOutputStream(java.io.OutputStream)

public DataOutputStream(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
Creates a new data output stream to write data to the specified underlying output stream. The counter written is set to zero.

Parameters:
out - the underlying output stream, to be saved for later use.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out
Method Detail

write sample code for java.io.DataOutputStream.write(int) definition code for java.io.DataOutputStream.write(int)

public void write(int b)
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes the specified byte (the low eight bits of the argument b) to the underlying output stream. If no exception is thrown, the counter written is incremented by 1.

Implements the write method of OutputStream.

Specified by:
write sample code for java.io.DataOutput.write(int) definition code for java.io.DataOutput.write(int) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Overrides:
write sample code for java.io.FilterOutputStream.write(int) definition code for java.io.FilterOutputStream.write(int) in class FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream
Parameters:
b - the byte to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out

write sample code for java.io.DataOutputStream.write(byte[], int, int) definition code for java.io.DataOutputStream.write(byte[], int, int)

public void write(byte[] b,
                  int off,
                  int len)
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes len bytes from the specified byte array starting at offset off to the underlying output stream. If no exception is thrown, the counter written is incremented by len.

Specified by:
write sample code for java.io.DataOutput.write(byte[], int, int) definition code for java.io.DataOutput.write(byte[], int, int) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Overrides:
write sample code for java.io.FilterOutputStream.write(byte[], int, int) definition code for java.io.FilterOutputStream.write(byte[], int, int) in class FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream
Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out

flush sample code for java.io.DataOutputStream.flush() definition code for java.io.DataOutputStream.flush()

public void flush()
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Flushes this data output stream. This forces any buffered output bytes to be written out to the stream.

The flush method of DataOutputStream calls the flush method of its underlying output stream.

Specified by:
flush sample code for java.io.Flushable.flush() definition code for java.io.Flushable.flush() in interface Flushable sample code for java.io.Flushable definition code for java.io.Flushable
Overrides:
flush sample code for java.io.FilterOutputStream.flush() definition code for java.io.FilterOutputStream.flush() in class FilterOutputStream sample code for java.io.FilterOutputStream definition code for java.io.FilterOutputStream
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out , OutputStream.flush() sample code for java.io.OutputStream.flush() definition code for java.io.OutputStream.flush()

writeBoolean sample code for java.io.DataOutputStream.writeBoolean(boolean) definition code for java.io.DataOutputStream.writeBoolean(boolean)

public final void writeBoolean(boolean v)
                        throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes a boolean to the underlying output stream as a 1-byte value. The value true is written out as the value (byte)1; the value false is written out as the value (byte)0. If no exception is thrown, the counter written is incremented by 1.

Specified by:
writeBoolean sample code for java.io.DataOutput.writeBoolean(boolean) definition code for java.io.DataOutput.writeBoolean(boolean) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Parameters:
v - a boolean value to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out

writeByte sample code for java.io.DataOutputStream.writeByte(int) definition code for java.io.DataOutputStream.writeByte(int)

public final void writeByte(int v)
                     throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes out a byte to the underlying output stream as a 1-byte value. If no exception is thrown, the counter written is incremented by 1.

Specified by:
writeByte sample code for java.io.DataOutput.writeByte(int) definition code for java.io.DataOutput.writeByte(int) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Parameters:
v - a byte value to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out

writeShort sample code for java.io.DataOutputStream.writeShort(int) definition code for java.io.DataOutputStream.writeShort(int)

public final void writeShort(int v)
                      throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes a short to the underlying output stream as two bytes, high byte first. If no exception is thrown, the counter written is incremented by 2.

Specified by:
writeShort sample code for java.io.DataOutput.writeShort(int) definition code for java.io.DataOutput.writeShort(int) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Parameters:
v - a short to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out

writeChar sample code for java.io.DataOutputStream.writeChar(int) definition code for java.io.DataOutputStream.writeChar(int)

public final void writeChar(int v)
                     throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes a char to the underlying output stream as a 2-byte value, high byte first. If no exception is thrown, the counter written is incremented by 2.

Specified by:
writeChar sample code for java.io.DataOutput.writeChar(int) definition code for java.io.DataOutput.writeChar(int) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Parameters:
v - a char value to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out

writeInt sample code for java.io.DataOutputStream.writeInt(int) definition code for java.io.DataOutputStream.writeInt(int)

public final void writeInt(int v)
                    throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes an int to the underlying output stream as four bytes, high byte first. If no exception is thrown, the counter written is incremented by 4.

Specified by:
writeInt sample code for java.io.DataOutput.writeInt(int) definition code for java.io.DataOutput.writeInt(int) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Parameters:
v - an int to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out

writeLong sample code for java.io.DataOutputStream.writeLong(long) definition code for java.io.DataOutputStream.writeLong(long)

public final void writeLong(long v)
                     throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes a long to the underlying output stream as eight bytes, high byte first. In no exception is thrown, the counter written is incremented by 8.

Specified by:
writeLong sample code for java.io.DataOutput.writeLong(long) definition code for java.io.DataOutput.writeLong(long) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Parameters:
v - a long to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out

writeFloat sample code for java.io.DataOutputStream.writeFloat(float) definition code for java.io.DataOutputStream.writeFloat(float)

public final void writeFloat(float v)
                      throws IOException sample code for java.io.IOException definition code for java.io.IOException 
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. If no exception is thrown, the counter written is incremented by 4.

Specified by:
writeFloat sample code for java.io.DataOutput.writeFloat(float) definition code for java.io.DataOutput.writeFloat(float) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Parameters:
v - a float value to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out , Float.floatToIntBits(float) sample code for java.lang.Float.floatToIntBits(float) definition code for java.lang.Float.floatToIntBits(float)

writeDouble sample code for java.io.DataOutputStream.writeDouble(double) definition code for java.io.DataOutputStream.writeDouble(double)

public final void writeDouble(double v)
                       throws IOException sample code for java.io.IOException definition code for java.io.IOException 
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. If no exception is thrown, the counter written is incremented by 8.

Specified by:
writeDouble sample code for java.io.DataOutput.writeDouble(double) definition code for java.io.DataOutput.writeDouble(double) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Parameters:
v - a double value to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out , Double.doubleToLongBits(double) sample code for java.lang.Double.doubleToLongBits(double) definition code for java.lang.Double.doubleToLongBits(double)

writeBytes sample code for java.io.DataOutputStream.writeBytes(java.lang.String) definition code for java.io.DataOutputStream.writeBytes(java.lang.String)

public final void writeBytes(String sample code for java.lang.String definition code for java.lang.String  s)
                      throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes out the string to the underlying output stream as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits. If no exception is thrown, the counter written is incremented by the length of s.

Specified by:
writeBytes sample code for java.io.DataOutput.writeBytes(java.lang.String) definition code for java.io.DataOutput.writeBytes(java.lang.String) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Parameters:
s - a string of bytes to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out

writeChars sample code for java.io.DataOutputStream.writeChars(java.lang.String) definition code for java.io.DataOutputStream.writeChars(java.lang.String)

public final void writeChars(String sample code for java.lang.String definition code for java.lang.String  s)
                      throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes a string to the underlying output stream as a sequence of characters. Each character is written to the data output stream as if by the writeChar method. If no exception is thrown, the counter written is incremented by twice the length of s.

Specified by:
writeChars sample code for java.io.DataOutput.writeChars(java.lang.String) definition code for java.io.DataOutput.writeChars(java.lang.String) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Parameters:
s - a String value to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.
See Also:
writeChar(int) sample code for java.io.DataOutputStream.writeChar(int) definition code for java.io.DataOutputStream.writeChar(int) , FilterOutputStream.out sample code for java.io.FilterOutputStream.out definition code for java.io.FilterOutputStream.out

writeUTF sample code for java.io.DataOutputStream.writeUTF(java.lang.String) definition code for java.io.DataOutputStream.writeUTF(java.lang.String)

public final void writeUTF(String sample code for java.lang.String definition code for java.lang.String  str)
                    throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.

First, two bytes are written to the output stream as if by the writeShort method giving the number of bytes to follow. This value is the number of bytes actually written out, not the length of the string. Following the length, each character of the string is output, in sequence, using the modified UTF-8 encoding for the character. If no exception is thrown, the counter written is incremented by the total number of bytes written to the output stream. This will be at least two plus the length of str, and at most two plus thrice the length of str.

Specified by:
writeUTF sample code for java.io.DataOutput.writeUTF(java.lang.String) definition code for java.io.DataOutput.writeUTF(java.lang.String) in interface DataOutput sample code for java.io.DataOutput definition code for java.io.DataOutput
Parameters:
str - a string to be written.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs.

size sample code for java.io.DataOutputStream.size() definition code for java.io.DataOutputStream.size()

public final int size()
Returns the current value of the counter written, the number of bytes written to this data output stream so far. If the counter overflows, it will be wrapped to Integer.MAX_VALUE.

Returns:
the value of the written field.
See Also:
written sample code for java.io.DataOutputStream.written definition code for java.io.DataOutputStream.written