BufferedOutputStream

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

What is the 32-bit representation of minus one?

Answer:

The dump shows that the last integer, -1, is represented as FF FF FF FF.

This is 32 one-bits, or 11111111111111111111111111111111

BufferedOutputStream

Here is the program again, this time with a BufferedOutputStream placed in the stream. In a program that writes only a few bytes, buffering adds little efficiency. This program writes 512*4 == 2048 bytes. Probably not really enough to worry about, but for practice let us 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.

QUESTION 8:

What do you suppose the method that writes a double is called?