我是刚刚用JAVA来做程序,所以数据库不熟,而且现在没时间去试验了,有其他方法吗???

解决方案 »

  1.   

    FileOutputStream(String filename, boolean append) append为true时为在文件里追加
      

  2.   

    你的意思是不是这样的?
    先建立一个 
    FileOutputStream file = new FileOutputStream("data.dat", true);
    然后再
    ObjectOutputStream objout = new ObjectOutputStream(file);这不就是添加了吗?
      

  3.   

    不是的吧,我怎么不可以,我是用先读一个,然后不关这个文件,再读第二个,它就显示eofexception,怎么回事呢。
      

  4.   

    是不是可以尝试用Hashtable,把数据按一个Object保存进Hashtable,再用ObjectOutputStream保存Hashtable.
      

  5.   

      void todisk() {
      try {
        ObjectOutputStream out =
          new ObjectOutputStream(
            new FileOutputStream("data.out",true));
        out.writeObject(this);
        out.close();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
      }
      Data fromdisk() {
      Data temp = new Data();
      Data temp1 = new Data();
      try {
        ObjectInputStream in =
          new ObjectInputStream(
            new FileInputStream("data.out"));
        temp1 = (Data)in.readObject();
        JOptionPane.showMessageDialog(null,"ok","ok",JOptionPane.INFORMATION_MESSAGE);
        temp = (Data)in.readObject();
        in.close();
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
      return temp;
      }
    }
    这两个函数,一个是serialize,另一个是deserialize,请帮我看看为什么读第二个值的时候出现eofexception错误啊。怎么样可以花最小代价解决这个问题呢??
      

  6.   

    要看看你的文件里面到底保存了多少个 Object。
    如果只保存一个的话,你读了一个之后再去读的话当然出错了。因为已经读完了呀。
      

  7.   

    拜托,不要说我不敬业好不好。:(
    我怎么知道你一定是写了多个才试的啊,我并不能够确信你文件里面的确保存了多个 Object 呀,因为你的异常提示时就是这样讲的。
      

  8.   

    这些是最主要的啦,你只要把Data换成你自己的定义的数据类型,就可以了。
      

  9.   

    以前没有碰到类似的情况,刚才研究了一下,是因为 ObjectOutputStream 在将 Object 写到文件的时候,会先写一个四字节的前导符,用来标记接下来的将是一个 Object 流(可以是很多个 Object 组成的流),但也只是在流的首部作这个标记。在中间的时候 writeObject 并不会产生这个标记。但是当我们采用 append 的方式打开 FileOutputStream 的时候,尽管现在已经是处于文件中间(不应写前导符),但对于 ObjectOutputStream 来说,它并不知道这么一回事,所以也将这个前导符号写了进去,所以我们按顺序读 Object 的时候,便会产生错误。这是我的一种临时解决方案,用了一个管道将前导符去掉,可能会很笨,不过一时没有想到其它办法。
    55~ 我水平不到家,用了太久的时间。import java.io.*;
    import java.util.*;public class SerializableObj implements Serializable
    {
    public static void main(String args[]) throws Exception
    {
        Obj obj = new Obj(); File file = new File("out.dat");
        obj.generalWriteObject("out.dat", "Hello, world!", file.exists());
        obj.generalWriteObject("out.dat", new Date(), file.exists());
            obj.readObject();
    }

    void generalWriteObject(String filename, Object obj, boolean append) throws Exception
    {
    java.io.PipedInputStream pipeIn = new java.io.PipedInputStream();
            java.io.PipedOutputStream pipeOut = new java.io.PipedOutputStream(pipeIn);
    java.io.ObjectOutputStream objout = new java.io.ObjectOutputStream(pipeOut);
    java.io.BufferedInputStream buffin = new java.io.BufferedInputStream(pipeIn);
    objout.writeObject(obj);
    objout.flush(); java.io.FileOutputStream fileOut = new java.io.FileOutputStream(filename, append);
    if (append) buffin.skip(4);
    int ch = -1;
    int count = buffin.available();
    int cur = 0;
    while (cur < count && (ch = buffin.read()) != -1){
    fileOut.write(ch);
    cur++;
    }
    } void readObject() throws Exception
    {
    Object obj = null;
    ObjectInputStream in 
    = new ObjectInputStream(new FileInputStream("out.dat")); try{
        while(true){
            obj = in.readObject();
            System.out.println(obj);
        }
    }catch(java.io.EOFException ex){
        // simply ignore it.
    } in.close();
    return;
    }