Flat File Generic Save/Load

Jan 2, 2021
Flat File Generic Save/Load
  • Code (Java):
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.Serializable;
    import java.util.zip.GZIPInputStream;
    import java.util.zip.GZIPOutputStream;

    import org.bukkit.util.io.BukkitObjectInputStream;
    import org.bukkit.util.io.BukkitObjectOutputStream;

    public class FlatFile {
        public static <T extends Serializable> boolean save(String filePath, T object) {
            try {
                BukkitObjectOutputStream out = new BukkitObjectOutputStream(new GZIPOutputStream(new FileOutputStream(filePath)));
                return true;
            } catch (IOException e) {
                return false;
        public static <T extends Serializable> T load(String filePath){
            try {
                BukkitObjectInputStream in = new BukkitObjectInputStream(new GZIPInputStream(new FileInputStream(filePath)));
                T object = (T) in.readObject();
                return object;
            } catch (ClassNotFoundException | IOException e) {
                return null;
    Code (Java):
    // Objects you are saving and loading must implement/extend Serializable or a Compilation Error will occur
        FlatFile.save(plugin.getDataFolder() + "/fileName.extension", objectToSave);
    // You must ensure that the data stored in the file being loaded corresponds to the object being loaded back or a Runtime Error will occur
        objectToLoad = FlatFile.load(plugin.getDataFolder() + "/fileName.extension");
  • Loading...
  • Loading...