What is the 32-bit representation of minus one?
The dump shows that the last integer, -1, is represented as FF FF FF FF
This is
32 one-bits, or 11111111111111111111111111111111
Here is the program again.
Now it writes out 512 ints.
A BufferedOutputStream is placed in the stream.
This buffers the bytes before they are written to disk.
Recall that a buffer is block of memory that is used to assemble
data before it is written out all at once.
(Somewhat like carrying your eggs in a basket rather than one by one.)
Buffering makes I/O operations more efficient. For a program that does massive I/O, buffering is essential. I/O is very slow compared to operations with main storage. Without buffering, I/O would be very, very slow. This program writes 512*4 == 2048 bytes, not really enough to worry about. But for practice, use a buffer.
import java.io.*;
class WriteInts
{
public static void main ( String[] args )
{
String fileName = "intData.dat" ;
try
{
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream( fileName )));
for ( int j=0; j<512; j++ )
out.writeInt( j );
out.close();
}
catch ( IOException iox )
{
System.out.println("Problem writing " + fileName );
}
}
}
DataOutputStream has methods for
writing out short, long,
double and other data.
What do you suppose the method that writes a double
is called?