Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Colegiu implements Serializable {
- public static void main(String[] args) throws ClassNotFoundException {
- int i;
- Item[] items = new Item[2];
- Colegiu c = new Colegiu();
- for (i = 0; i < items.length; i++) {
- items[i] = c.new Item();
- }
- items[0].setItemID("ITEM101");
- items[1].setItemID("ITEM102");
- items[0].setDesc("iPad");
- items[1].setDesc("iPhone");
- items[0].setCost(499);
- items[1].setCost(599);
- items[0].setQuantity(1);
- items[1].setQuantity(3);
- System.out.println("Item Detalii.....");
- for (Item d : items) {
- System.out.print(d.getItemID());
- System.out.print("\t" + d.getDesc());
- System.out.print("\t" + d.getCost());
- System.out.println("\t" + d.getQuantity());
- }
- List<Item> obj;
- obj = new ArrayList<Item>();
- for (i = 0; i < items.length; i++) {
- obj.add(items[i]);
- }
- // Sa facem serializarea unui obiect
- try {
- FileOutputStream fileOut = new FileOutputStream("out.txt");
- ObjectOutputStream out = new ObjectOutputStream(fileOut);
- out.writeObject(obj);
- out.close();
- fileOut.close();
- System.out.println("\nSerializare Succes ... Verificati fisierul de iesire specificat ..\n");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // Aici deserializam un Obiect
- try {
- FileInputStream fileIn = new FileInputStream("out.txt");
- ObjectInputStream in = new ObjectInputStream(fileIn);
- System.out.println("Datele deserializate: \n" + in.readObject().toString());
- in.close();
- fileIn.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public class Item implements Serializable {
- private String itemID;
- private String desc;
- private double cost;
- private int quantity;
- public Item() {
- itemID = "";
- desc = "";
- cost = 0;
- quantity = 0;
- }
- public Item(String id, String d, double c, int q) {
- itemID = id;
- desc = d;
- cost = c;
- quantity = q;
- }
- public String getItemID() {
- return itemID;
- }
- public void setItemID(String itemID) {
- this.itemID = itemID;
- }
- public String getDesc() {
- return desc;
- }
- public void setDesc(String desc) {
- this.desc = desc;
- }
- public double getCost() {
- return cost;
- }
- public void setCost(double cost) {
- this.cost = cost;
- }
- public int getQuantity() {
- return quantity;
- }
- public void setQuantity(int quantity) {
- this.quantity = quantity;
- }
- public String toString() {
- return "Item [itemID=" + itemID + ", desc=" + desc + ", cost=" + cost + ", quantity=" + quantity + "]";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement