Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 把ArrayList放入dat文件中,ObjectOutput对象中的问题?
nihaolaogao





发贴: 8
积分: 0
于 2008-11-16 09:25 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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




初学者与<书>

话题树型展开
人气 标题 作者 字数 发贴时间
9455 把ArrayList放入dat文件中,ObjectOutput对象中的问题? nihaolaogao 3466 2008-11-16 09:25
8199 Re:把ArrayList放入dat文件中,ObjectOutput对象中的问题? xuxiaolei 377 2008-11-16 11:14

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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