Created
October 18, 2018 09:40
-
-
Save Juansero29/19546d2fe8c619a42fc5d39756001e69 to your computer and use it in GitHub Desktop.
A simple binary persistence manager written in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This file and every class, interface and other components in this project is licenses | |
* under 'THE UNLICENSE' terms and conditions. | |
* | |
* THE UNLICENSE: | |
* This is free and unencumbered software released into the public domain. | |
* | |
* Anyone is free to copy, modify, publish, use, compile, sell, or | |
* distribute this software, either in source code form or as a compiled | |
* binary, for any purpose, commercial or non-commercial, and by any | |
* means. | |
* | |
* In jurisdictions that recognize copyright laws, the author or authors | |
* of this software dedicate any and all copyright interest in the | |
* software to the public domain. We make this dedication for the benefit | |
* of the public at large and to the detriment of our heirs and | |
* successors. We intend this dedication to be an overt act of | |
* relinquishment in perpetuity of all present and future rights to this | |
* software under copyright law. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
* OTHER DEALINGS IN THE SOFTWARE. | |
* | |
* For more information, please refer to <http://unlicense.org/> | |
*/ | |
package data; | |
import models.HellPassHospital; | |
import models.Room; | |
import java.io.*; | |
/** | |
* Persists objects in a binary file | |
* | |
* @author Juan | |
* Created on: 2018 - October - 05 | |
*/ | |
public class PersistenceManager { | |
//region Utility Methods | |
/** | |
* Serializes this object | |
* | |
* @param o - the object to serialize | |
*/ | |
public static void Save(Object o) { | |
try (ObjectOutputStream fos = new ObjectOutputStream(new FileOutputStream("myRoom"))) { | |
fos.writeObject(o); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Deserialize a {@link HellPassHospital} | |
* | |
* @return - the deserialized hospital | |
* @throws IOException - if there's a problem with the file | |
* @throws ClassNotFoundException - if the class of the object can't be found | |
*/ | |
public static HellPassHospital ReadHellPassHospital() throws IOException, ClassNotFoundException { | |
try (ObjectInputStream fis = new ObjectInputStream(new FileInputStream("myRoom"))) { | |
return (HellPassHospital) (fis.readObject()); | |
} | |
} | |
//endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment