user1079322
user1079322

Reputation: 13

Java Programming: Storing Objects and Arrays into a file

*My assignment is to write a program that stores an array of five int values 1,2,3,4, and 5, a Date object for current time, and a double value 5.5. into a file named Exercies19_2.txt. Next, I am to write a method in the program that opens Exercises19_2.txt, reads the data and displays to the command window. When I run my program I keep getting this:

Exception in thread "main" java.io.OptionalDataException at java.io.ObjectInputStream.readObject0(Unknown Source) at java.io.ObjectInputStream.readObject(Unknown Source) at WriteArray.main(WriteArray.java:29).

Can you help me figure out what I am doing wrong????? Here is what I have written:

    import java.io.*;

    public class WriteArray {

    public static void main(String[] args)throws ClassNotFoundException,IOException {
    int[] numbers = {1, 2, 3, 4, 5};

    //Create an output stream for file
    ObjectOutputStream output = new ObjectOutputStream
    (new FileOutputStream("Exercises19_2.txt", true));

    //Write to file
      output.writeDouble(5.5);
      output.writeObject(numbers);
      output.writeObject(new java.util.Date());
      output.writeUTF("Exercises19_2.txt");

    //Close the stream
      output.close();

     //Create an input stream for file
     ObjectInputStream input = new ObjectInputStream
           (new FileInputStream("Exercises19_2.txt"));

     int[]newNumbers = (int[])(input.readObject());
       for (int i = 0; i < newNumbers.length; i++)
         System.out.println("Integers: " + newNumbers[i] + " ");

    String FileName= input.readUTF();
    double Double = input.readDouble();
    java.util.Date date = (java.util.Date)(input.readObject());
      System.out.println("DateTime: " + date);


    //Display the output

    System.out.println("Double: " + " " + input.readDouble());
    System.out.println("FileName: " + " " + input.readUTF());

    //Close the stream
     input.close();


      }
    }

Upvotes: 0

Views: 7183

Answers (4)

MockerTim
MockerTim

Reputation: 2483

You should read the answers carefully next time.

aix, Chris and Mike Adler gave you the exact hint: read back in the same order as you've written into the file.

After that, the solution is very easy:

import java.io.*;
import java.util.Arrays;
import java.util.Date;

public class WriteArray {

    public static void main(String[] args) 
        throws ClassNotFoundException, IOException {
        int[] numbers = {1, 2, 3, 4, 5};

        //Create an output stream for file
        ObjectOutputStream output 
            = new ObjectOutputStream(
                  new FileOutputStream("Exercises19_2.bin", true));

        //Write to file
        // 1. Write double
        output.writeDouble(5.5);
        // 2. Write int array object
        output.writeObject(numbers);
        // 3. Write date object
        output.writeObject(new java.util.Date());
        // 4. Write utf string
        output.writeUTF("Exercises19_2.bin");

        // Close the stream
        output.close();

        //Create an input stream for file
        ObjectInputStream input 
            = new ObjectInputStream(
                  new FileInputStream("Exercises19_2.bin"));

        // Read from file
        // 1. Read double
        double doubleValue = input.readDouble();
        System.out.println("Double value: " + doubleValue);
        // 2. Read int array object
        int[] newNumbers = (int[]) (input.readObject());
        System.out.println("Integers: " + Arrays.toString(newNumbers));
        // 3. Read date object
        Date date = (java.util.Date) (input.readObject());
        System.out.println("DateTime: " + date);
        // 4. Read utf string
        String fileName = input.readUTF();
        System.out.println("File name: " + fileName);

        // Close the stream
        input.close();
    }
}

Upvotes: 1

Mike Adler
Mike Adler

Reputation: 1200

Try reading back in the same order as you've written into the file (double, object, date).

Upvotes: 3

Chris
Chris

Reputation: 23171

When serializing and deserializing to an objectOutputStream, you have to read in the objects in the same order you wrote them. The first thing you wrote to the file was a double with output.writeDouble(5.5); so you need to read the double back in before you can read the numbers array.

Upvotes: 3

NPE
NPE

Reputation: 500267

Each output.writeXXX() call writes something to the output file. When reading the data back, the sequence of reads must exactly correspond to the sequence of writes. Anything else won't work.

P.S. The fact that the file name Exercies19_2.txt ends in .txt hints that it ought to be a text file (i.e. readable by a human being). ObjectOutputStream does not produce a text file; it produces a binary one. You might want to check with your instructor to see what they're expecting.

Upvotes: 2

Related Questions