Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.foi.nwtis.mbenotic.konfiguracije;
- import com.google.gson.Gson;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Properties;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- class KonfiguracijaJSON extends KonfiguracijaApstraktna {
- public KonfiguracijaJSON(String datoteka) {
- super(datoteka);
- }
- @Override
- public void spremiKonfiguraciju() throws NemaKonfiguracije, NeispravnaKonfiguracija {
- spremiKonfiguraciju(this.datoteka);
- }
- @Override
- public void ucitajKonfiguraciju() throws NemaKonfiguracije, NeispravnaKonfiguracija {
- ucitajKonfiguraciju(this.datoteka);
- }
- @Override
- public void ucitajKonfiguraciju(String datoteka) throws NemaKonfiguracije, NeispravnaKonfiguracija {
- this.obrisiSvePostavke();
- if (datoteka == null || datoteka.length() == 0) {
- throw new NemaKonfiguracije("Neispravni naziv datoteke!");
- }
- File f = new File(datoteka);
- if (f.exists() && f.isFile()) {
- try {
- Gson gson = new Gson();
- BufferedReader br = new BufferedReader(new FileReader(f));
- this.postavke = gson.fromJson(br, Properties.class);
- br.close();
- } catch (IOException ex) {
- throw new NeispravnaKonfiguracija("Problem kod citanja datoteke!");
- }
- } else {
- throw new NemaKonfiguracije("Ne postoji datoteka!");
- }
- }
- @Override
- public void spremiKonfiguraciju(String datoteka) throws NemaKonfiguracije, NeispravnaKonfiguracija {
- if (datoteka == null || datoteka.length() == 0) {
- throw new NemaKonfiguracije("Neispravni naziv datoteke!");
- }
- File f = new File(datoteka);
- if ((f.exists() && f.isFile()) || !f.exists()) {
- try {
- Gson gson = new Gson();
- String json = gson.toJson(this.postavke);
- FileWriter fileWriter = new FileWriter(f);
- fileWriter.write(json);
- fileWriter.close();
- } catch (IOException ex) {
- throw new NeispravnaKonfiguracija("Problem kod pisanja datoteke!");
- }
- } else {
- throw new NemaKonfiguracije("Ne postoji datoteka!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement