Skip to content

Instantly share code, notes, and snippets.

@strangeglyph
Created October 19, 2011 18:19
Show Gist options
  • Save strangeglyph/1299183 to your computer and use it in GitHub Desktop.
Save strangeglyph/1299183 to your computer and use it in GitHub Desktop.
Yaml
/**
* Save the object in form of a .yml file
*
* @param o The object
* @param filename The filename (not including file ending and path)
*/
public void save(Object o, String filename) {
try {
File f = new File(cb.getDataFolder() + "/" + filename + ".yml");
if (!f.exists()) {
f.createNewFile();
}
yaml.dump(o, new OutputStreamWriter(new FileOutputStream(f)));
} catch (IOException ioe) {
MsgTools.log(Level.SEVERE, "Unexpected IOError while trying to save object " + o + ", the object might not have been saved", ioe);
}
}
/**
* Loads an object from a .yml file
*
* @param filename The filename (not including file ending and path)
* @return The object saved in that file
*/
public Object load(String filename) {
File f = new File(cb.getDataFolder() + "/" + filename + ".yml");
if (!f.exists()) {
return null;
}
try {
return yaml.load(new InputStreamReader(new FileInputStream(f)));
} catch (FileNotFoundException fileNotFoundException) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment