Skip to content

Instantly share code, notes, and snippets.

@conikeec
Created June 14, 2018 14:03
Show Gist options
  • Save conikeec/094812807f6c924a7991c83fbd76624b to your computer and use it in GitHub Desktop.
Save conikeec/094812807f6c924a7991c83fbd76624b to your computer and use it in GitHub Desktop.
class Utils
{
// Function to serialize an object and write it to a file
public static void SerializeToFile(Object obj, String filename)
{
try
{
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
// Serialization of the object to file
System.out.println("Serializing " + obj.toString() + " to " + filename);
out.writeObject(obj);
out.close();
file.close();
}
catch(Exception e)
{
System.out.println("Exception: " + e.toString());
}
}
// Function to deserialize an object from a file
public static Object DeserializeFromFile(String filename)
{
Object obj = new Object();
try
{
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
// Deserialization of the object to file
System.out.println("Deserializing from " + filename);
obj = in.readObject();
in.close();
file.close();
}
catch(Exception e)
{
System.out.println("Exception: " + e.toString());
}
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment