import
java.io.Serializable; public class Pers implements Serializable{ String name; int age; Pers(String name,int age){ this.name = name; this.age = age; } } |
import
java.io.*; public class ArrPers implements Serializable{ int size; Pers arr[]; ArrPers(){ arr = new Pers[5]; size = 0; } void add(Pers p){ if(size >= arr.length-1){ Pers temp[] = new Pers[2*arr.length]; System.arraycopy(arr, 0, temp, 0, arr.length); arr=temp; } arr[size++]=p; } void prt(){ System.out.println ("Arrpers: "+size+" objets"); for(int i=0;i<this.size;i++){ System.out.println("name: "+arr[i].name+"\tage:"+ arr[i].age); } } void remove (Pers p){ for(int i=0;i<size;i++){ if (p==arr[i]){ arr[i]=arr[--size]; break; } } } void save(){ try{ FileOutputStream fos = new FileOutputStream ("save.dat"); ObjectOutputStream oos = new ObjectOutputStream (fos); oos.writeObject(this); } catch(Exception ex){} } static ArrPers load(){ ArrPers p= null; try{ FileInputStream fis = new FileInputStream ("save.dat"); ObjectInputStream ois = new ObjectInputStream (fis); p= (ArrPers)ois.readObject(); } catch(Exception ex){} return p; } } |
public
class Test { public static void main(String arg[]){ ArrPers p =null; p= ArrPers.load(); if(p == null) p = new ArrPers(); p.prt(); for(int i=0;i<3;i++){ String name ="client"+(int)(Math.random()*1000); int age = (int)(Math.random()*80); p.add(new Pers(name,age)); } p.prt(); p.save(); } } |