java - Should YamlConfiguration objects be closed? -
i've been working on plugin requires fair amount of data being stored. have being stored in custom config file found online works same default config.
the problem i'm having not sure how close file or if need to, know little yaml configurations. code template used below.
i'm curious advice on how should store larger amounts of data in future.
public class customconfig { //store name of file load/edit private final string filename; //store plugin, file directory private final javaplugin plugin; //store actual hard disk file location private file configfile; //store ram file copy location private fileconfiguration fileconfiguration; //constructor taking plugin , filename public customconfig(javaplugin plugin, string filename) { //ensure plugin exists folder path if (plugin == null) throw new illegalargumentexception("plugin cannot null"); //set classes plugin variable 1 passed method this.plugin = plugin; //get name of file load/edit this.filename = filename; //get directory/folder of file load/edit file datafolder = plugin.getdatafolder(); if (datafolder == null) throw new illegalstateexception(); //load config file hard disk this.configfile = new file(plugin.getdatafolder(), filename); reloadconfig(); } public void reloadconfig() { //load memory file hard copy fileconfiguration = yamlconfiguration.loadconfiguration(configfile); // defaults in jar file configfile = new file(plugin.getdatafolder(), filename); if (configfile != null) { yamlconfiguration defconfig = yamlconfiguration.loadconfiguration(configfile); fileconfiguration.setdefaults(defconfig); } } public fileconfiguration getconfig() { if (fileconfiguration == null) { this.reloadconfig(); } return fileconfiguration; } public void saveconfig() { if (fileconfiguration == null || configfile == null) { return; } else { try { getconfig().save(configfile); } catch (ioexception ex) { plugin.getlogger().log(level.severe, "could not save config " + configfile, ex); } } } public void savedefaultconfig() { if (!configfile.exists()) { this.plugin.saveresource(filename, false); } } }
no. not have close yamlconfiguration
objects.
while default config (javaplugin.getconfig()
) bound lifecycle of plugin, custom ones disposed when other java object is, i.e. when garbage collector determines there no more references pointing them in code.
Comments
Post a Comment