Package org.onebusaway.utility
Class ObjectSerializationLibrary
- java.lang.Object
-
- org.onebusaway.utility.ObjectSerializationLibrary
-
public class ObjectSerializationLibrary extends Object
Convenience methods for serializing objects to and from streams and files.- Author:
- bdferris
-
-
Constructor Summary
Constructors Constructor Description ObjectSerializationLibrary()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ObjectInputStreamgetFileAsObjectInputStream(File file)Convenience method to open a specified file as anObjectInputStreamstatic ObjectOutputStreamgetFileAsObjectOutputStream(File file)Convenience method to open a specified file as anObjectOutputStreamstatic voidmain(String[] args)static voidprintObject(File file, Object o)Convenience method to serialize the string representation of an object, as returned by theObject.toString()method, to a file.static <T> TreadObject(File file)Read an object from a file by opening the file as anObjectInputStreamand reading a single object from the stream.static voidwriteObject(File file, Object o)Serialize an object to a file by opening anObjectOutputStreamlinked to the file and writing the object to the stream.
-
-
-
Method Detail
-
main
public static void main(String[] args) throws IOException, ClassNotFoundException
- Throws:
IOExceptionClassNotFoundException
-
getFileAsObjectOutputStream
public static ObjectOutputStream getFileAsObjectOutputStream(File file) throws IOException
Convenience method to open a specified file as anObjectOutputStream- Parameters:
file- the target output file- Returns:
- an ObjectOutputStream linked to the target file
- Throws:
IOException
-
getFileAsObjectInputStream
public static ObjectInputStream getFileAsObjectInputStream(File file) throws IOException
Convenience method to open a specified file as anObjectInputStream- Parameters:
file- the target input file- Returns:
- an ObjectInputStream linked to the target file
- Throws:
IOException
-
writeObject
public static void writeObject(File file, Object o) throws IOException
Serialize an object to a file by opening anObjectOutputStreamlinked to the file and writing the object to the stream.- Parameters:
file- the target output fileo- the object to serialize- Throws:
IOException
-
readObject
public static <T> T readObject(File file) throws IOException, ClassNotFoundException
Read an object from a file by opening the file as anObjectInputStreamand reading a single object from the stream.- Parameters:
file- the input file- Returns:
- an object deserialized from the file
- Throws:
IOExceptionClassNotFoundException
-
printObject
public static void printObject(File file, Object o) throws IOException
Convenience method to serialize the string representation of an object, as returned by theObject.toString()method, to a file.- Parameters:
file- the target output fileo- the object to serialize- Throws:
IOException
-
-