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(); } } |
import
java.io.*; public class Copy { public static void main(String arg[]) { String f1="",f2=""; try{ f1=arg[0]; f2=arg[1]; }catch (IndexOutOfBoundsException e){ System.out.println("Usage: java Copy file_1 file_2"); System.exit(1); } try { BufferedReader in = new BufferedReader( new FileReader(f1)); BufferedWriter ot = new BufferedWriter( new FileWriter(f2)); System.out.println("Copy "+arg[0]+" as "+arg[1]); String tampon="" ; while(( tampon=in.readLine()) != null) { ot.write(tampon) ; ot.newLine(); } in.close(); ot.close(); System.out.println("done"); } catch(FileNotFoundException ef){ System.out.print(arg[0]+" does not exist"); System.out.println(" or "+arg[1]+ " can not be created"); System.exit(3); } catch(IOException e) { e.printStackTrace(); System.exit(4); } } } |
import java.io.FileInputStream; |
import java.io.FileInputStream; |