Text and Binary Files
Binary files
Write to a binary file
Common methods of the DataOutput interface
Method | Throws | Description |
---|---|---|
writeBoolean(boolean) | IOException | Writes a 1-byte boolean value to the output stream. |
writeInt(int) | IOException | Writes a 4-byte int value. |
writeDouble(double) | IOException | Writes an 8-byte double value. |
writeChar(int) | IOException | Writes a 2-byte char value. |
writeChars(String) | IOException | Writes a string using 2 bytes per character. |
writeUTF(String) | IOException | Writes a 2-byte value for the number of bytes in the string followed by the UTF representation of the string. |
Methods of the DataOutputStream class
Method | Throws | Description |
---|---|---|
size() | None | Returns an int for the number of bytes written to this stream. |
flush() | IOException | Flushes any data that's in the buffer to the file. |
close() | IOException | Flushes any data that's in the buffer to the file and closes the stream. |
Code that writes data to a binary file
// write a Product object to the file out.writeUTF(product.getCode()); out.writeUTF(product.getDescription()); out.writeDouble(product.getPrice()); // flush data to the file and close the output stream out.close();