Untitled

 avatar
unknown
plain_text
a month ago
1.1 kB
4
Indexable
public void exportGameData() {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("Save Game Data");
    fileChooser.setSelectedFile(new File("reigns_decisions.json"));

    int userSelection = fileChooser.showSaveDialog(null);
    if (userSelection == JFileChooser.APPROVE_OPTION) {
        File fileToSave = fileChooser.getSelectedFile();
        try {
            // Read existing data from game_progress.json
            List<Map<String, Object>> history = new Gson().fromJson(new FileReader(saveFile), List.class);
            if (history == null) history = new ArrayList<>();
            
            // Save to chosen location
            FileWriter writer = new FileWriter(fileToSave);
            new GsonBuilder().setPrettyPrinting().create().toJson(history, writer);
            writer.close();

            JOptionPane.showMessageDialog(null, "Game data exported successfully!");
        } catch (IOException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "Error exporting data.");
        }
    }
}
Editor is loading...
Leave a Comment