Skip to content

Instantly share code, notes, and snippets.

@jirkapenzes
Created May 5, 2012 15:26
Show Gist options
  • Save jirkapenzes/2603292 to your computer and use it in GitHub Desktop.
Save jirkapenzes/2603292 to your computer and use it in GitHub Desktop.
Simple save/load serializable class
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