Created
June 14, 2018 14:03
-
-
Save conikeec/094812807f6c924a7991c83fbd76624b to your computer and use it in GitHub Desktop.
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
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