Advertisement
Talar97

Zapis/odczyt obiektów z pliku

Jan 31st, 2018
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. Main.java:
  2. package com.Talar;
  3.  
  4. import java.io.*;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.         Telefon nadawca = new Telefon("Samsung", "Note","600444777",2506,3560);
  10.         Telefon odbiorca = new Telefon("HTC", "Desire 820","500333666",1235,600);
  11.  
  12.         //nadawca.wyswietlInformacje();
  13.         //odbiorca.wyswietlInformacje();
  14.  
  15.         //nadawca.wyslijSms(odbiorca, "Chuj ci na pysk pedale");
  16.  
  17.  
  18.         //Zapis obiektu do pliku
  19.         FileOutputStream fos = null;
  20.         ObjectOutputStream oos = null;
  21.  
  22.         try{
  23.             fos = new FileOutputStream("telefony.dat", true);
  24.             try{
  25.                 oos = new ObjectOutputStream(fos);
  26.                 oos.writeObject(nadawca);
  27.                 oos.writeObject(odbiorca);
  28.                 oos.close();
  29.             }catch(IOException e) {System.out.println(e);}
  30.         }catch(FileNotFoundException e) {System.out.println(e);}
  31.  
  32.         //Odczyt obiektu z pliku
  33.         FileInputStream fis = null;
  34.         ObjectInputStream ois = null;
  35.         Telefon pobrany = null;
  36.         try{
  37.             fis = new FileInputStream("telefony.dat");
  38.             try{
  39.                 ois = new ObjectInputStream(fis);
  40.                 while(true){
  41.                     pobrany = (Telefon)ois.readObject();
  42.                     pobrany.wyswietlInformacje();
  43.                 }
  44.             }catch(Exception e) {System.out.println(e);}
  45.         }catch(FileNotFoundException e) { System.out.println(e);}
  46.     }
  47. }
  48.  
  49.  
  50.  
  51. -----------------------------
  52. Telefon.java
  53. -----------------------------
  54.  
  55. package com.Talar;
  56.  
  57. import java.io.Serializable;
  58.  
  59. public class Telefon implements Serializable{
  60.     private String producent;
  61.     private String model;
  62.     private String numer;
  63.     private int id;
  64.     private double cena;
  65.  
  66.  
  67.     public Telefon(String prod, String mod, String num, int id, double cena){
  68.         this.producent = prod;
  69.         this.model = mod;
  70.         this.numer = num;
  71.         this.id = id;
  72.         this.cena = cena;
  73.     }
  74.  
  75.     public void wyslijSms(Telefon odbiorca, String wiadomosc){
  76.         System.out.println("Wiadomość z numeru: " + this.getNumer() + " na numer " + odbiorca.getNumer() + ":");
  77.         System.out.println(wiadomosc);
  78.     }
  79.  
  80.     public void wyswietlInformacje(){
  81.         System.out.println("Producent: " + this.producent + "" +
  82.                 "\nModel: " + this.model + "" +
  83.                 "\nNumer: " + this.numer + "" +
  84.                 "\nId: " + this.numer + "" +
  85.                 "\nCena: " + this.cena + "\n");
  86.     }
  87.  
  88.     public String getProducent() {
  89.         return producent;
  90.     }
  91.  
  92.     public String getModel() {
  93.         return model;
  94.     }
  95.  
  96.     public String getNumer() {
  97.         return numer;
  98.     }
  99.  
  100.     public int getId() {
  101.         return id;
  102.     }
  103.  
  104.     public double getCena() {
  105.         return cena;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement