title: How to write and read dat file
body: <blockquote>
    <p>Write a program that asks the user to enter whole numbers (they enter
    –1 to finish data entry). The numbers are to be written to a binary
    file. This is to be done in a method called <code>writeBinaryFile()</code>. Then
    the program should read the numbers from the binary file and display
    them to the console. This should be done in a method called
    <code>readBinaryFile()</code>. The <code>main()</code> method should just call <code>writeBinaryFile()</code>
    then <code>readBinaryFile()</code>.</p>
  </blockquote>
  
  <p>My code so far:</p>
  
  <pre><code>public static void main(String[] args) throws FileNotFoundException, IOException {
      System.out.println("Please enter whole numbers then enter -1 to data entry.");
      writeBinaryFile();
      readBinaryFile();
  }
  
  /**
   * This method opens a binary file and writes the contents
   * of an int array to the file.
   */
  private static void writeBinaryFile() throws IOException, FileNotFoundException
  {
      Scanner input = new Scanner(System.in);
      ArrayList&lt;Integer&gt; number = new ArrayList&lt;Integer&gt;();
      try
      {
          FileOutputStream output = new FileOutputStream("Files//Numbers.dat");
          int numbers = 0;
  
          while(numbers != -1)
          {
              number.add(numbers = input.nextInt());
  
              output.write(numbers);
          }
          output.close();
      }
      catch(IOException ex)
      {
          ex.getMessage();
      }
  }
  
  private static void readBinaryFile() throws IOException
  {
      FileInputStream input = new FileInputStream("Files//Numbers.dat");
      int value;
      value = input.read();
      System.out.print("The numbers in the file are:  ");
      try
      {
          while(value != -1)
          {
              System.out.print(value +" ");
              value = input.read();
          }
          input.close();
      }
      catch(IOException ex)
      {
          ex.getMessage();
      }
  }
  </code></pre>
  
  <p>The problem is that when I enter this data:</p>
  
  <blockquote>
    <p>5 2 4 6 8  4 -1</p>
  </blockquote>
  
  <p>My output is:</p>
  
  <blockquote>
    <p>5 2 4 6 8  4 255 </p>
  </blockquote>
  
  <p>How can I stop 255 from coming up?</p>