yxhxj2006

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          Java對(duì)象的持久化

          Java對(duì)象的持久化主要使用ObjectInputStream與ObjectOutputStream類 來(lái)實(shí)現(xiàn)!


          public class Student implements Serializable {
           
                  String name;
                  int id ;
               int age;
                  String department;
            public Student(String name, int id, int age, String department) {
             this.age = age;
             this.department = department;
             this.id = id;
             this.name = name;
            }

          public static void saveObjects(Student student, String fileName) { 
          FileOutputStream os = new FileOutputStream("fileName.dat"); 
          ObjectOutputStream oos = new ObjectOutputStream(os); 
          oos.writeObject(student); 
          }

          public static Student readObjects(String fileName) { 
          Student student; 
          Object obj; 
          try
          FileInputStream is = new FileInputStream("fileName.dat"); 
          ObjectInputStream ois = new ObjectInputStream(is); 
          obj = ois.readObject(); 
          }catch (Exception e) { 
          e.printStackTrace(); 

          if(obj instanceof Student){ 
          Student stu= (Student)obj; 
          return stu; 

          return null
          }

          posted on 2013-08-09 01:30 奮斗成就男人 閱讀(261) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 青冈县| 双流县| 沁水县| 五家渠市| 丰台区| 桂林市| 凤台县| 贵州省| 邢台县| 光山县| 新兴县| 合水县| 三门县| 宁海县| 敖汉旗| 大宁县| 湘潭县| 建始县| 札达县| 德阳市| 大余县| 阿尔山市| 溧水县| 昭苏县| 冕宁县| 友谊县| 巍山| 泽库县| 沂南县| 新营市| 涟水县| 奉节县| 柏乡县| 绥宁县| 康保县| 丰县| 自治县| 江城| 定襄县| 南和县| 客服|