| import
java.io.Serializable; public class Pers implements Serializable{ private static final long serialVersionUID = 1L; String name; int age; Pers(String name,int age){ this.name = name; this.age = age; } } |
| import
java.io.*; public class ArrPers implements Serializable{ private static final long serialVersionUID = 1L; 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 ("\nArrpers contains: "+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); oos.close(); } catch(Exception ex){ System.out.println("the file save.dat cannot be created"); return; } } static ArrPers load(){ ArrPers p= null; try{ FileInputStream fis = new FileInputStream ("save.dat"); ObjectInputStream ois = new ObjectInputStream (fis); p= (ArrPers)ois.readObject(); ois.close(); } catch(Exception ex){ System.out.println("the file save.dat is not found"); } return p; } } |
| public
class Test { public static void main(String arg[]){ ArrPers p =null; p= ArrPers.load(); if(p == null) { p = new ArrPers(); System.out.println("New array is created"); } else { System.out.println("The array is loaded:"); } p.prt(); System.out.println("\nAdding three persons to the array"); 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(); } } |
| class MyClass
implements Serializable { transient Thread thread; //Skip serialization of the transient field transient String fieldIdontwantSerialization; static int number; //Skip serialization of the statis field // Serialize the rest of the fields int data; String x; // More code } |
| import java.io.Serializable; public class PersonSr implements Serializable{ private static final long serialVersionUID = 1L; static int num=0; String name; String id; PersonSr(){ name = "student "+ ++num; id = (int)(Math.random()*3999)+1111+""; } } public class StudentSr extends PersonSr{ private static final long serialVersionUID = 1L; int notes[]; StudentSr(){ notes = new int [5]; for(int i =0;i<notes.length;i++){ notes[i]=(int)(Math.random()*5)+2; } } public String toString(){ String rez; rez = "name:"+name+"\tid:"+id+"\tnotes:"; for(int i=0;i<notes.length;i++){ rez += "\t"+notes[i]; } return rez; } } |
| import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class StudWr { public static void main(String arg[]) throws IOException{ ObjectOutputStream oos = null; StudentSr s; try{ oos = new ObjectOutputStream ( new FileOutputStream ("save.ser")); for(int i=0;i<9;i++){ s =new StudentSr(); System.out.print(""+s); oos.writeObject(s); System.out.println(" # is serialized"); } } finally{ oos.close(); } } } |
| import
java.io.EOFException; import java.io.FileInputStream; import java.io.IOException; import java.io.FileNotFoundException; import java.io.ObjectInputStream; public class StudRd { public static void main(String[] arg)throws IOException,ClassNotFoundException{ ObjectInputStream ois = null; StudentSr s; int n=0, nf=0; try{ ois = new ObjectInputStream ( new FileInputStream ("save.ser")); for(;;){ s= (StudentSr)ois.readObject(); n++; if(average(s)>=4){ System.out.print(""+s); System.out.println("\thas average "+ average(s)); nf++; } } } catch (FileNotFoundException fne) { System.out.println("There is no file save.ser"); } catch(EOFException ex){ System.out.println("printed "+nf+" students, found total "+n+" students"); } finally{ if(ois!=null)ois.close(); } } public static double average(StudentSr s){ double sum; int i; for(sum=i=0;i<s.notes.length;i++){ sum+=s.notes[i]; } return sum/s.notes.length; } } |
| 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; |