Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main.java:
- package com.Talar;
- import java.io.*;
- public class Main {
- public static void main(String[] args) {
- Telefon nadawca = new Telefon("Samsung", "Note","600444777",2506,3560);
- Telefon odbiorca = new Telefon("HTC", "Desire 820","500333666",1235,600);
- //nadawca.wyswietlInformacje();
- //odbiorca.wyswietlInformacje();
- //nadawca.wyslijSms(odbiorca, "Chuj ci na pysk pedale");
- //Zapis obiektu do pliku
- FileOutputStream fos = null;
- ObjectOutputStream oos = null;
- try{
- fos = new FileOutputStream("telefony.dat", true);
- try{
- oos = new ObjectOutputStream(fos);
- oos.writeObject(nadawca);
- oos.writeObject(odbiorca);
- oos.close();
- }catch(IOException e) {System.out.println(e);}
- }catch(FileNotFoundException e) {System.out.println(e);}
- //Odczyt obiektu z pliku
- FileInputStream fis = null;
- ObjectInputStream ois = null;
- Telefon pobrany = null;
- try{
- fis = new FileInputStream("telefony.dat");
- try{
- ois = new ObjectInputStream(fis);
- while(true){
- pobrany = (Telefon)ois.readObject();
- pobrany.wyswietlInformacje();
- }
- }catch(Exception e) {System.out.println(e);}
- }catch(FileNotFoundException e) { System.out.println(e);}
- }
- }
- -----------------------------
- Telefon.java
- -----------------------------
- package com.Talar;
- import java.io.Serializable;
- public class Telefon implements Serializable{
- private String producent;
- private String model;
- private String numer;
- private int id;
- private double cena;
- public Telefon(String prod, String mod, String num, int id, double cena){
- this.producent = prod;
- this.model = mod;
- this.numer = num;
- this.id = id;
- this.cena = cena;
- }
- public void wyslijSms(Telefon odbiorca, String wiadomosc){
- System.out.println("Wiadomość z numeru: " + this.getNumer() + " na numer " + odbiorca.getNumer() + ":");
- System.out.println(wiadomosc);
- }
- public void wyswietlInformacje(){
- System.out.println("Producent: " + this.producent + "" +
- "\nModel: " + this.model + "" +
- "\nNumer: " + this.numer + "" +
- "\nId: " + this.numer + "" +
- "\nCena: " + this.cena + "\n");
- }
- public String getProducent() {
- return producent;
- }
- public String getModel() {
- return model;
- }
- public String getNumer() {
- return numer;
- }
- public int getId() {
- return id;
- }
- public double getCena() {
- return cena;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement