Created
May 5, 2012 15:26
-
-
Save jirkapenzes/2603292 to your computer and use it in GitHub Desktop.
Simple save/load serializable class
This file contains hidden or 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
package core.io; | |
import java.io.*; | |
/** | |
* Author: Jirka Pénzeš | |
* Date: 4.5.12 23:55 | |
*/ | |
public class Serializer { | |
public static <T> boolean Save(T data, String fileName) { | |
try { | |
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(fileName)); | |
output.writeObject((Object) data); | |
output.close(); | |
return true; | |
} catch (IOException ex) { | |
return false; | |
} | |
} | |
public static <T> T Load(String fileName) { | |
T data; | |
try { | |
ObjectInputStream input = new ObjectInputStream(new FileInputStream(fileName)); | |
data = (T) input.readObject(); | |
input.close(); | |
} catch (IOException ex) { | |
return null; | |
} catch (ClassNotFoundException ex) { | |
return null; | |
} | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment