The following
classes mentioned on this site can use DataOutput
's methods: DataOutputStream
class, ObjectOutputStream
class, and RandomAccessFile
class
■ DataOutput is the interface which defines basic methods for the byte output stream classes listed above. This section describes those methods.
■ The DataOutput interface defines fourteen methods for converting Java primitives and String types to raw bytes and then writing them to a byte stream.
■ The DataOutput methods are:
write( int ) - Writes the eight low-order bits of an int.
write( byte[ ] ) - Writes all the bytes in a byte array.
write( byte[ ], offset, len ) - Writes len bytes from a byte array starting at offset off.
writeBoolean( boolean ) - Writes a one-byte boolean value.
writeByte( int ) - Acts the same as write( int b ) above
writeBytes( String ) - Writes the low order bytes of all the characters in a String.
writeChar( int ) - Writes a two-byte char value from the low end of an int.
writeChars( String ) - Writes the two char bytes of all the characters in a String.
writeDouble( double ) - Writes an eight-byte double value.
writeFloat( float ) - Writes a four-byte float value.
writeInt( int ) - Writes a four-byte int value.
writeLong( long ) - Writes an eight-byte long value.
writeShort( int ) - Writes a two byte short value from the low end of an int.
writeUTF( String ) - Writes the characters of the String in special modified UTF format so readUTF(...) can read them back.
■ All of the above methods have corresponding read(...) methods under DataInput. So anything written here can always be re-read and reconstructed exactly across environments using these methods. See DataInput interface.
■ All of DataOutput‘s methods can throw IOExceptions.
■ This method writes the eight low-order bits of the int. Its high 24 bits are ignored.
■ Assuming that dos is already a valid DataOutputStream object reference, the following example will write the ten hex bytes "0102030405060708090A". Only the low order byte of each int is written.
import
java.io.*;
try
{
for ( int x = 1; x < 11; x++ ) {
dos.write( x );
}
}
catch
(IOException e) { }
■ This method writes to the stream all the bytes which are currently residing in array b.
■ Nothing is written if the array’s length is zero. However if it is null then a NullPointerException is thrown.
■ This snippet writes the three hex bytes "414243"
from array b.
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream( "new_output_filename" );
DataOutputStream dos = new
DataOutputStream( fos );
byte[ ] b = new byte[ ]{ (byte) 0x41,
(byte) 0x42, (byte) 0x43 };
dos.write( b );
dos.close(
);
}
catch
(IOException e) { }
■ Writes the ten hex bytes "0102030405060708090A".
import
java.io.*;
try
{
FileOutputStream
fos = new FileOutputStream( "my_new_output_filename" );
int
i[ ] = {1,2,3,4,5,6,7,8,9,10};
for
(int x = 0; x < i.length; x++) {
fos.write( i[x] ); // write a byte
}
fos.close(
);
} catch (IOException e) { }
void .write(
byte[ ], offset, len
) method
■ This method writes len bytes from array b, starting at offset offset.
■ The snippet below is similar to one above except that it just writes the two hex bytes "4445" from the middle of array b.
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream("new_output_file" );
DataOutputStream dos = new
DataOutputStream( fos );
byte[ ] b = new byte[ ]{ (byte) 0x41,
(byte) 0x42, (byte) 0x43, (byte) 0x44, (byte) 0x45, (byte) 0x46 };
dos.write( b, 3, 2 );
}
catch (IOException e) { }
void .writeBoolean(
boolean ) method
■ This method writes a one-byte value of hex "01" if the boolean argument is true. It writes hex "00" if the argument is false. i.e. If dos is a valid DataOutputStream object reference:
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream("new_test_file" );
DataOutputStream dos = new
DataOutputStream( fos );
dos.writeBoolean( true ); //
writes hex "01"
dos.writeBoolean( false ); //
now write hex "00" also
dos.close( );
}
catch (IOException e) { }
■ This method behaves the same as public void write( int ) above. The low order 8 bits of the int are written. See write( int ) above for an example.
void
.writeBytes(
String ) method
■ This method writes the low-end 8 bits of each character from a String. The high eight bits of each character are ignored.
■ Since the first 127 characters of Unicode UTF-16 and UTF-8 both correspond to the same characters in ASCII, writeBytes(...) effectively converts internal Java String characters into 8 bit ASCII bytes.
■ Assuming dos is a valid DataOutputStream object reference, both writeBytes(...) statements below write six hex bytes "414243444546", which correspond to the ASCII values for A through F.
import
java.io.*;
try
{
FileOutputStream fos = new FileOutputStream("new_writeBytes_test_file"
);
DataOutputStream dos = new
DataOutputStream( fos );
String s = "ABCDEF";
dos.writeBytes( s );
// or, the same thing could be stated this way:
// dos.writeBytes( "ABCDEF" );
}
catch (IOException e) { }
■ Variable length record files. The snippet below provides a method called writeVarRcd which accepts a String object and a DataOutputStream object as its two parameters. The method presumes the stream is available for writing. It uses writeBytes(...) to write the passed String as a variable length record into the file, preceeding it with a two-byte binary length field for that record. If successful, it returns the length of the record written. If unsuccessful, it returns -1.
For a method called readVarRcds
which reads back this entire file back into a Vector,
see the readFully(...) method examples under DataInputStream
class.
import
java.io.*;
public int
writeVarRcd( String s,
DataOutputStream dos ) {
try
{
short x = (short) s.length( );
dos.writeShort( x ); //
write the 2-byte length
dos.writeBytes( s ); //
write the data
return x; //
return how many were written
}
catch
(IOException e) { return -1; } //
return -1 if exception
}
■ This method writes the low-end 16 bits of an int. The high 16 bits are ignored.
■ Data written by this method is meant to be read by its byte input stream counterpart, readChar(...). Doing so can provide accurate and consistent transfer of Java chars across machine environments.
■ The first snippet below writes six characters from a char array to a file and the second snippet reads them back in as the exact same chars again.
// Write them out:
import
java.io.*;
try {
FileOutputStream fos = new
FileOutputStream( "chars_filename" );
DataOutputStream dos = new
DataOutputStream( fos );
char[ ] c = { 'a', 'b', 'c', 'd',
'e', '\u5555' };
int y;
for ( int x = 0; x < 6; x++ ) {
y = c[ x ];
dos.writeChar( y ); // write them
out
}
dos.close( );
} catch (IOException e) { }
//
Read them back:
import
java.io.*;
try {
FileInputStream fis = new
FileInputStream( "chars_filename" );
DataInputStream dis = new
DataInputStream( fis );
char[ ] d = new char[ 6 ];
for ( int x = 0; x < 6; x++ ) {
d[ x ] = dis.readChar( ); // read them in
}
dis.close( );
} catch (IOException e) { }
■ Assuming dos is already a valid DataOutputStream object reference, the code below will write the series of 10 two-byte hex values "0001 0002 0003 0004 0005 0006 0007 0008 0009 FFFF". Note that saying 0xFFFF or 0xFFFFFF or 0xFFFFFFFF in the final statement will achieve this same result. Saying 0xFF in the final statement will not, as 0xFF does not completely "fill" the low end two bytes of the promoted int with all ones.
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream( "chars_filename" );
DataOutputStream dos = new
DataOutputStream( fos );
for ( int x = 1; x <=9; x++ ) {
dos.writeChar( x );
}
dos.writeChar( 0xFFFF );
}
catch
(IOException e) { }
void .writeChars( String ) method
■ This method writes both bytes of each character which resides in String s. It creates a file of 16-bit characters.
■ Using writeChars(...) to write Strings will distort subsequent printing of its output. To write just the low end byte of each character and thus produce printable ASCII, you should use the writeBytes(...) method instead.
■ Data written by this method is meant to be read by its byte input stream counterpart, readChar( ). Doing so can provide accurate and consistent transfer of Java Strings across machine environments.
■ The following writeChars(...) snippet produces the hex output "00 48 00 65 00 6C 00 6C 00 6F" writing two bytes per character. Text editors will perceive this as "H e l l o", with spaces between the letters, not "Hello".
import
java.io.*;
String
s = "Hello";
try {
FileOutputStream fos = new
FileOutputStream( "my_new_output_filename" );
DataOutputStream dos = new DataOutputStream( fos );
dos.writeChars( s );
}
catch ( IOException e) { }
■ Note there is no readChars( ) method. To retrieve data written with writeChars(...) you must use readChar( ) which reads one character at a time. You may add buffering with a BufferedInputStream.
void .writeDouble( double ) method
■ This method writes an eight byte double.
■ Data written by this method is meant to be read by its byte input
stream counterpart, readDouble(...).
■ Assuming dos is a valid DataOutputStream object reference, the snippet below will write the hex output "00 00 00 00 00 00 00 00 3F F0 00 00 00 00 00 00 40 00 00 00 00 00 00 00" which is the floating point representation of 0, 1, 2 as doubles.
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream( "my_doubles_output_filename" );
DataOutputStream dos = new DataOutputStream( fos );
for ( double d = 0; d < 3.0; d++ ) {
dos.writeDouble( d );
}
}
catch (IOException e) { }
void .writeFloat( float ) method
■ This method writes a four byte float.
■ Data written by this method is meant to be read by its byte input
stream counterpart, readFloat(...).
■ Assuming dos is a valid DataOutputStream object reference, the snippet below will write the hex output "00 00 00 00 3F F0 00 00 40 00 00 00 40 40 00 00" which is the floating point representation of 0, 1, 2, 3 as floats.
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream( "my_float_output_filename" );
DataOutputStream dos = new DataOutputStream( fos );
for ( float f = 0f; f < 4.0; f++ ) {
dos.writeFloat( f );
}
}
catch (IOException e) { }
■ This method writes a four byte int.
■ Data written by this method is meant to be read by its byte input stream counterpart, readInt(...).
■ Assuming dos is a valid DataOutputStream object reference, the snippet below will write the hex output "00 00 00 00 00 00 00 01 00 00 00 02 00 00 00 03" which is 0, 1, 2, 3 as four-byte ints.
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream( "my_int_output_filename" );
DataOutputStream dos = new DataOutputStream( fos );
for ( int x = 0; x < 4; x++ ) {
dos.writeInt( x );
}
}
catch (IOException e) { }
void .writeLong( long ) method
■ This method writes an eight byte long.
■ Data written by this method is meant to be read by its byte input
stream counterpart, readLong(...).
■ Assuming dos is a valid DataOutputStream object reference, the snippet below will write the hex output "00 00 00 00 00 00 00 05 00 00 00 00 00 00 00 06 00 00 00 00 00 00 00 07" which is 5, 6, 7 as eight-byte longs.
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream( "my_long_output_filename" );
DataOutputStream dos = new DataOutputStream( fos );
for ( long l = 5; l < 8; l++ ) {
dos.writeLong( l );
}
}
catch (IOException e) { }
void .writeShort( int ) method
■ This method writes a two byte short.
■ Data written by this method is meant to be read by its byte input
stream counterpart, readShort(...).
■ Assuming dos is a valid DataOutputStream object reference, the snippet below will write the hex output "00 09 00 0A 00 0B" which is 9, 10, 11 as two-byte shorts.
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream( "my_short_output_filename" );
DataOutputStream dos = new DataOutputStream( fos );
for ( short sh = 9; sh < 12; sh++ ) {
dos.writeShort( sh );
}
}
catch (IOException e) { }
void .writeUTF( String ) method
■ This method writes the characters of the String in a special one, two, or three byte modified UTF format so that the method readUTF(...) can read them back and reconstruct the String.
■ The method throws a UTFDataFormatException if the total bytes submitted to be written in one operation exceed 65,535.
■ Since in the default locale String low end byte values from 00 to 7f correspond to ASCII, and this 00-7f range is written as one byte per character by writeUTF(...), not two or three bytes, it means that writeUTF(...) can produce a legible 8-bit text file from a String. A file leadoff length indicator of two bytes would have to be ignored here.
■ The following snippet takes a 16-character String of "A" through "P" and writes the hex output "00 10 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50", which looks like A through P. The initial "00 10" is the two-byte modified UTF length indicator of 16. Since each of the sixteen characters was less than hex 7f, each occupies one byte in the subsequent output.
import
java.io.*;
try
{
FileOutputStream fos = new
FileOutputStream("output_UTF_filename");
DataOutputStream dos = new DataOutputStream(
fos );
String s = "ABCDEFGHIJKLMNOP";
dos.writeUTF( s );
}
catch (IOException e) { }