#Saving Data to File Problem

1 messages · Page 1 of 1 (latest)

worldly snow
#
public class Database implements Serializable {

    @Serial
    private static transient final long serialVersionUID = -1681012206529286330L;

    public static final String filePath = "database.ser";

    private ItemStack[] activeItems;
    private ItemStack[] collectedItems;
    private ItemStack[] disabledItems;
    ...
    public boolean saveData(String filePath) {
        Bukkit.getServer().getLogger().log(Level.INFO, "Trying to save...");
        try {
            FileOutputStream fileOut = new FileOutputStream(filePath);
            BukkitObjectOutputStream out = new BukkitObjectOutputStream(fileOut);
            out.writeObject(this);
            out.close();
            Bukkit.getServer().getLogger().log(Level.INFO, "Data saved!");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            Bukkit.getServer().getLogger().log(Level.INFO, "!SAVING FAILED!");
            return false;
        }
    }
    ...
}

Hey,
im trying to save the entire Database Class with the active,collected and disabledItems arrays into a file. Is this the right way to go about it? Its loosely based on this https://www.spigotmc.org/wiki/save-load-data-files/
If i save the data this way no file gets created :/ Does anyone see why ?

narrow flare
#

is the file even created?

worldly snow
#

no

#

should i create the file myself ?

narrow flare
#

then create it iguess

worldly snow
#

i figured the out.writeObject would take care of it since its not throwing an error or anything

south echo
#

what's even shown on the console tho?

#

did your debug message get printed?

worldly snow
sage dew
#

Why saving data in that way?

worldly snow
#

Cause i googled it and that guide was the first result to come up ^^

south echo
narrow flare
#

lmao

sage dew
#

I would use Json or Yaml

narrow flare
#

serializing itemstacks

worldly snow
south echo
narrow flare
#

you can serialize itemstacks

south echo
#

ik, but I guess not passing them as objects of an OutputStream

worldly snow
#

whats the proper way to store data then ?

south echo
worldly snow
#

i know how to use databases

#

but for storing so little data a database is total overkill

#

im just trying to store a handfull of items...

#

i will look into the config files