Java编程:将对象和数组存储到文件中

时间:2011-12-03 19:40:51

标签: java arrays serialization

*我的任务是编写一个程序,它存储一个包含五个int值1,2,3,4和5的数组,一个Date对象用于当前时间,以及一个double值5.5。到名为Exercies19_2.txt的文件中。接下来,我将在程序中编写一个方法,打开Exercises19_2.txt,读取数据并显示到命令窗口。当我运行我的程序时,我会继续这样做:

  

java.io.ObjectInputStream.readObject0(未知来源)中线程“main”java.io.OptionalDataException中的异常   at java.io.ObjectInputStream.readObject(Unknown Source)   在WriteArray.main(WriteArray.java:29)。

你可以帮我弄明白我做错了吗?????这是我写的:

    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();


      }
    }

4 个答案:

答案 0 :(得分:3)

在对objectOutputStream进行序列化和反序列化时,必须按照编写它们的顺序读入对象。您写入文件的第一件事是带有output.writeDouble(5.5);的双精度因此,您需要在读取数字数组之前阅读双重输入。

答案 1 :(得分:3)

尝试以与写入文件(double,object,date)相同的顺序读回。

答案 2 :(得分:2)

每个output.writeXXX()调用都会向输出文件写入内容。当读回数据时,读取序列必须与写入序列完全对应。其他任何东西都行不通。

P.S。文件名Exercies19_2.txt.txt结尾的事实暗示它应该是文本文件(即,人类可读)。 ObjectOutputStream不会生成文本文件;它产生二进制的。您可能需要咨询教师,了解他们的期望。

答案 3 :(得分:1)

下次你应仔细阅读答案。

aixChrisMike Adler为您提供了确切的提示:按照您写入文件的相同顺序回读。

之后,解决方案非常简单:

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();
    }
}