Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. public class Cwiczenie31 {
  2.  
  3. public static void main(String[] args) {
  4. try {
  5. zapiszOsoby();
  6. odczytajOsoby();
  7. } catch (IOException ex) {
  8. ex.printStackTrace();
  9. } catch (ClassNotFoundException ex) {
  10. ex.printStackTrace();
  11. }
  12. }
  13.  
  14. public static void zapiszOsoby() throws IOException {
  15. ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("osoby.data"));
  16.  
  17. Set<Osoba> osoby = new HashSet<>();
  18. osoby.add(new Osoba(15, "Jan", 82.5f));
  19. osoby.add(new Osoba(25, "Monika", 65));
  20. osoby.add(new Osoba(31, "Krzysztof", 72));
  21.  
  22. out.writeObject(osoby);
  23. }
  24.  
  25. public static void odczytajOsoby() throws IOException, ClassNotFoundException {
  26. ObjectInputStream in = new ObjectInputStream(new FileInputStream("osoby.data"));
  27.  
  28. Set<Osoba> osoby = (Set<Osoba>) in.readObject();
  29.  
  30. for (Osoba osoba : osoby) {
  31. System.out.println(osoba.getImie());
  32. }
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement