go to previous page   go to home page   go to next page

Will the following statement work?

DataOutputStream dataOut = new DataOutputStream( "myFile.dat" );

Answer:

No. You need a FileOutputStream to connect to a disk file, then connect a DataOutputStream to the FileOutputStream.


DataOutputStream

DataOutputStream is used to write bytes representing primitive data types. It writes to another output stream typically BufferedOutputStream or FileOutputStream.


Constructor

public DataOutputStream(OutputStream out)
    Construct a data output stream.

Methods

public void flush() throws IOException
    — Flushes the stream, ie. makes sure all data reaches its destination.

public final int size()
    — Return the number of bytes written so far.

public void write(int b) throws IOException
    — Writes the low-order eight bits of the argument.

public final void writeBoolean(boolean b) throws IOException
    — Writes a boolean as a 1-byte value. 
    — True is written as 00000001, false is written as 00000000.

public void writeBytes(String s) throws IOException
    — Write out the low eight bits of each character of the string.

public void writeDouble(double v) throws IOException

public void writeFloat(float f) throws IOException

public void writeInt(int i) throws IOException

public void writeLong(long l) throws IOException

public final void writeShort(int s) throws IOException

There are many more methods than these. See the on-line documentation for a complete list.

Bug Alert: The argument to write() is an int. The low eight bits of the argument are written to the stream.


QUESTION 11:

Say that out is a DataOutputStream. How many bytes will the following write?

out.writeBoolean( true );
out.writeBoolean( false );

(Look at the documentation before answering.)