printstream and objectoutputstream code example

Example: objectoutputstream exemple

public static void main(String[] args) throws IOException, ClassNotFoundException {
  Obj array[] = new Obj[10];
  for (int i = 0; i < array.length; i++) {
    array[i] = new Emp(i);
  }
  try {
    ObjectOutputStream fOUT = new ObjectOutputStream(new FileOutputStream("prodotti.dat"));
    for (int i = 0; i < array.length; i++) {
      fOUT.writeObject(array[i]);
    }
    fOUT.close();
  } catch (IOException e) {
    System.out.println("Errore nella scrittura");
  }
  ObjectInputStream fIN = new ObjectInputStream(new FileInputStream("prodotti.dat"));
  while (true) {
    try {
      Obj p = (Obj) fIN.readObject();
      System.out.println(Obj.method());
    } catch (EOFException e) {
      break;
    }
  }
}

Tags:

Java Example