Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Reader;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- public class json {
- private double balance;
- private double newBalance;
- public json(double newBalance) throws IOException {
- this.newBalance = new Account().setBalance();
- }
- public json() {
- this.balance = balance;
- }
- public double getBalanceJson() {
- Reader reader = null;
- try {
- reader = new FileReader("./src/account.json");
- JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
- balance = jsonObject.get("balance").getAsDouble();
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return balance;
- }
- public void setBalanceJson() throws IOException {
- Reader reader = null;
- try {
- reader = new FileReader("./src/account.json");
- JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
- jsonObject.addProperty("balance", newBalance);
- FileWriter fileWriter = new FileWriter("./src/account.json");
- fileWriter.write(jsonObject.toString());
- fileWriter.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment