Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. package databaza;
  2.  
  3.  
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7.  
  8. public class FileOperations {
  9.  
  10.     static String fileName = "zamestnanci.dat";
  11.     static String fileName2 = "prace.dat";
  12.     static String fileName3 = "zamByType.dat";
  13.     static String fileName4 = "maxUvazek.dat";
  14.  
  15.     public static void saveToFile(HashMap<String, Employee> map, TreeMap<String, Integer> prace, HashMap<String, Integer> zamByType, Integer maxUvazek) {
  16.  
  17.         try {
  18.             FileOutputStream fos = new FileOutputStream(fileName);
  19.             ObjectOutputStream oos = new ObjectOutputStream(fos);
  20.             oos.writeObject(map);
  21.             oos.close();
  22.             fos.close();
  23.             //System.out.printf("Zamestnanci serializovani.\n");
  24.  
  25.             FileOutputStream fos2 = new FileOutputStream(fileName2);
  26.             ObjectOutputStream oos2 = new ObjectOutputStream(fos2);
  27.             oos2.writeObject(prace);
  28.             oos2.close();
  29.             fos2.close();
  30.             //System.out.printf("Prace serializovana.\n");
  31.  
  32.             FileOutputStream fos3 = new FileOutputStream(fileName3);
  33.             ObjectOutputStream oos3 = new ObjectOutputStream(fos3);
  34.             oos3.writeObject(zamByType);
  35.             oos3.close();
  36.             fos3.close();
  37.             //System.out.printf("ZamByType serializovano.\n");
  38.  
  39.             FileOutputStream fos4 = new FileOutputStream(fileName4);
  40.             ObjectOutputStream oos4 = new ObjectOutputStream(fos4);
  41.             oos4.writeObject(maxUvazek);
  42.             oos4.close();
  43.             fos4.close();
  44.             //System.out.printf("MaxUvazek serializovan.\n");
  45.  
  46.         } catch (IOException ioe) {
  47.             ioe.printStackTrace();
  48.         }
  49.     }
  50.  
  51.     public static SerializedObjectsAbstraction loadFromFile(HashMap<String, Employee> map, TreeMap<String, Integer> prace, HashMap<String, Integer> zamByType, Integer maxUvazek) {
  52.  
  53.         try {
  54.             FileInputStream fis = new FileInputStream(fileName);
  55.             ObjectInputStream ois = new ObjectInputStream(fis);
  56.             map = (HashMap) ois.readObject();
  57.             ois.close();
  58.             fis.close();
  59.  
  60.             FileInputStream fis2 = new FileInputStream(fileName2);
  61.             ObjectInputStream ois2 = new ObjectInputStream(fis2);
  62.             prace = (TreeMap) ois2.readObject();
  63.             ois2.close();
  64.             fis2.close();
  65.  
  66.             FileInputStream fis3 = new FileInputStream(fileName3);
  67.             ObjectInputStream ois3 = new ObjectInputStream(fis3);
  68.             zamByType = (HashMap) ois3.readObject();
  69.             ois3.close();
  70.             fis3.close();
  71.  
  72.             FileInputStream fis4 = new FileInputStream(fileName4);
  73.             ObjectInputStream ois4 = new ObjectInputStream(fis4);
  74.             maxUvazek = (Integer) ois4.readObject();
  75.             ois4.close();
  76.             fis4.close();
  77.         }catch (FileNotFoundException e){
  78.             System.out.println("Nebyl nalezen soubor s ulozenymi daty.");
  79.         } catch (IOException ioe) {
  80.             ioe.printStackTrace();
  81.             return null;
  82.         } catch (ClassNotFoundException c) {
  83.             System.out.println("Class not found");
  84.             c.printStackTrace();
  85.             return null;
  86.         }
  87.  
  88.         for (Map.Entry<String, Employee> m : map.entrySet()) {
  89.             System.out.println(m.getKey() + " : " + m.getValue());
  90.         }
  91.  
  92.         for (Map.Entry<String, Integer> prc : prace.entrySet()) {
  93.             System.out.println(prc.getKey() + " : " + prc.getValue());
  94.         }
  95.  
  96.         System.out.println("Maximalni uvzek: " + maxUvazek);
  97.  
  98.         SerializedObjectsAbstraction abstraction = new SerializedObjectsAbstraction(map, prace, zamByType, maxUvazek);
  99.         return abstraction;
  100.     }
  101.  
  102.     public static class SerializedObjectsAbstraction {
  103.         public final HashMap<String, Employee> map;
  104.         public final TreeMap<String, Integer> prace;
  105.         public final HashMap<String, Integer> zamByType;
  106.         public final Integer maxUvazek;
  107.  
  108.         public SerializedObjectsAbstraction(HashMap<String, Employee> map, TreeMap<String, Integer> prace, HashMap<String, Integer> zamByType, Integer maxUvazek) {
  109.             this.map = map;
  110.             this.prace = prace;
  111.             this.zamByType = zamByType;
  112.             this.maxUvazek = maxUvazek;
  113.         }
  114.  
  115.         public HashMap<String, Employee> getEmployeeMap() {
  116.             return map;
  117.         }
  118.  
  119.         public TreeMap<String, Integer> getPraceMap() {
  120.             return prace;
  121.         }
  122.  
  123.         public HashMap<String, Integer> getZamByTypeMap() {
  124.             return zamByType;
  125.         }
  126.  
  127.         public Integer getMaxUvazek() {
  128.             return maxUvazek;
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement