Advertisement
matokc95

JSONKonfiguracija

Mar 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package org.foi.nwtis.mbenotic.konfiguracije;
  2.  
  3. import com.google.gson.Gson;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.FileReader;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.util.Properties;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. class KonfiguracijaJSON extends KonfiguracijaApstraktna {
  17.  
  18.     public KonfiguracijaJSON(String datoteka) {
  19.         super(datoteka);
  20.     }
  21.  
  22.     @Override
  23.     public void spremiKonfiguraciju() throws NemaKonfiguracije, NeispravnaKonfiguracija {
  24.         spremiKonfiguraciju(this.datoteka);
  25.     }
  26.  
  27.     @Override
  28.     public void ucitajKonfiguraciju() throws NemaKonfiguracije, NeispravnaKonfiguracija {
  29.         ucitajKonfiguraciju(this.datoteka);
  30.     }
  31.  
  32.     @Override
  33.     public void ucitajKonfiguraciju(String datoteka) throws NemaKonfiguracije, NeispravnaKonfiguracija {
  34.         this.obrisiSvePostavke();
  35.  
  36.         if (datoteka == null || datoteka.length() == 0) {
  37.             throw new NemaKonfiguracije("Neispravni naziv datoteke!");
  38.         }
  39.         File f = new File(datoteka);
  40.         if (f.exists() && f.isFile()) {
  41.             try {
  42.                 Gson gson = new Gson();
  43.                 BufferedReader br = new BufferedReader(new FileReader(f));
  44.                 this.postavke = gson.fromJson(br, Properties.class);
  45.                 br.close();
  46.             } catch (IOException ex) {
  47.                 throw new NeispravnaKonfiguracija("Problem kod citanja datoteke!");
  48.             }
  49.         } else {
  50.             throw new NemaKonfiguracije("Ne postoji datoteka!");
  51.         }
  52.     }
  53.  
  54.     @Override
  55.     public void spremiKonfiguraciju(String datoteka) throws NemaKonfiguracije, NeispravnaKonfiguracija {
  56.         if (datoteka == null || datoteka.length() == 0) {
  57.             throw new NemaKonfiguracije("Neispravni naziv datoteke!");
  58.         }
  59.         File f = new File(datoteka);
  60.         if ((f.exists() && f.isFile()) || !f.exists()) {
  61.             try {
  62.                 Gson gson = new Gson();
  63.                 String json = gson.toJson(this.postavke);
  64.                 FileWriter fileWriter = new FileWriter(f);
  65.                 fileWriter.write(json);
  66.                 fileWriter.close();
  67.             } catch (IOException ex) {
  68.                 throw new NeispravnaKonfiguracija("Problem kod pisanja datoteke!");
  69.             }
  70.         } else {
  71.             throw new NemaKonfiguracije("Ne postoji datoteka!");
  72.         }
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement