Deserialize an object from a file with Java.

	public static Object deserializeObject (String fileName)
    {
    	Object object = null;
    	try {
    		File f = new File(fileName);
    		if (!f.exists())
    			return null;
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
			object = ois.readObject();
			ois.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
    	
    	return object;
    }

And to test it…

	public static void main(String[] args) {
		MyObject myObject = (MyObject)deserializeObject("C:\\path\\to\\my\\file.dat");
	}

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.
$2.00
$5.00
Other

Related Posts

Tagged with:  

Leave a Reply