Topic: 把ArrayList放入dat文件中,ObjectOutput对象中的问题?

  Print this page

1.把ArrayList放入dat文件中,ObjectOutput对象中的问题? Copy to clipboard
Posted by: nihaolaogao
Posted on: 2008-11-16 09:25

package com.xml;
import java.io.Serializable;
//类schedule
public class schedule implements Serializable {
  int id;
  String type;
  String day;
  String time;
  String content;

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public String getDay() {
    return day;
  }

  public void setDay(String day) {
    this.day = day;
  }

  public String getTime() {
    return time;
  }

  public void setTime(String time) {
    this.time = time;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

}
///////////////////////////////////////
package com.xml;

import java.io.IOException;

import com.xml.objectOperImpl;
import com.xml.schedule;
//objecttest主程序
public class objecttest {
  /**
   * @param args
   */
  public static void main(String[] args) throws IOException {
    objectOperImpl ob = new objectOperImpl();
    schedule s = new schedule();
    s.setType("sdgg");
    s.setTime("gggggggg");
    ob.createObjectMoon;

    schedule d = new schedule();
    d.setType("aaaaaaasssssssss");
    d.setTime("fdsfdsfdsfds");
    ob.createObjectFood;
    
    
    schedule sss = new schedule();
    sss.setType("aaaaaaabbbbbbbbb");
    sss.setTime("fdsfdsfdsfds");
    ob.createObject(sss);

    System.out.println(ob.readObjects().size());
    
  //   ob.deleteObject(sss);

      System.out.println(ob.readObjects().size());
  }

}
////////////////////////////////////////////////////
package com.xml;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
//业务处理
public class objectOperImpl{
  public ArrayList<schedule> scheList = null;
  File file = new File("object.dat");
  FileOutputStream out;
  ObjectOutputStream objout;
public static int account = 1;
  

  
  public boolean createObject(schedule sche) {
    try {
      out = new FileOutputStream(file,false);
      objout = new ObjectOutputStream(out);
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    try {
      scheList = readObjects();
      scheList.add(sche);
      System.out.println("-----"+scheList.size());
      objout.writeObject(scheList);
      //objout.flush();
      objout.close();
      out.close();
      return true;
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
  }

//  

  public ArrayList<schedule> readObjects() {
    FileInputStream in;
    ObjectInputStream objin;
    ArrayList<schedule> sches = new ArrayList<schedule>();
    try {
      in = new FileInputStream("object.dat");
      objin = new ObjectInputStream(in);
      //if (objin.readObject() != null)
       if(account == 1) {
        
         account ++;
        
       }else{
        sches = (ArrayList<schedule>Wink objin.readObject();
        System.out.println(sches.size());
        
       }
      objin.close();
      in.close();
    }catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return sches;
  }

}
写到文件中,再读出来,ArrayList的Size()总是1

2.Re:把ArrayList放入dat文件中,ObjectOutput对象中的问题? [Re: nihaolaogao] Copy to clipboard
Posted by: xuxiaolei
Posted on: 2008-11-16 11:14

public FileOutputStream(File file, boolean append) throws FileNotFoundException
append - if true, then bytes will be written to the end of the file rather than the beginning

out = new FileOutputStream(file,false);
第二个参数设置成false表示不追加而是重新覆盖这个文件,这样你每次只写一个(如果文件以前有内容会覆盖掉),读出来当然也是一个了,把 out = new FileOutputStream(file,false);写成 out = new FileOutputStream(file,true);
应该就可以了吧


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923