yxhxj2006

          常用鏈接

          統計

          最新評論

          Java對象的持久化

          Java對象的持久化主要使用ObjectInputStream與ObjectOutputStream類 來實現!


          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) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 巴中市| 西藏| 安平县| 双城市| 洱源县| 绥滨县| 耒阳市| 靖州| 尼玛县| 贞丰县| 日土县| 台江县| 大英县| 京山县| 土默特左旗| 鸡泽县| 左云县| 柳林县| 芒康县| 会同县| 集安市| 开阳县| 鄂托克前旗| 库尔勒市| 宁乡县| 海兴县| 从化市| 林芝县| 泰兴市| 莒南县| 通海县| 孟村| 新民市| 虹口区| 凤台县| 泰宁县| 烟台市| 蚌埠市| 郯城县| 湘阴县| 广东省|