Revision as of 04:16, 23 November 2010 by Han84 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
public static Object convertFileToObject(String filename) {
	return convertByteArrayToObject(convertFileToByteArray(filename));
}
 
public static byte[] convertFileToByteArray(String filename) {
	java.io.File f = new java.io.File(filename);
	java.io.FileInputStream fis = null;
	byte[] stream = new byte[(int) f.length()];
	try {
		fis = new java.io.FileInputStream(f);
	} catch (java.io.FileNotFoundException ex) {
		Ts.printErr(ex);
	}
	try {
		fis.read(stream);
		fis.close();
	} catch (java.io.IOException ex) {
		Ts.printErr(ex);
	}
	return stream;
}
 
public static Object convertByteArrayToObject(byte[] stream) {
	java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(stream);
	java.io.ObjectInputStream ois = null;
	Object obj = null;
	try {
		ois = new java.io.ObjectInputStream(bais);
		try {
			obj = ois.readObject();
		} catch (ClassNotFoundException ex) {
			Ts.printErr(ex);
		}
		ois.close();
		bais.close();
	} catch (java.io.IOException ex) {
		Ts.printErr(ex);
	}
	return obj;
}
 
public static void printErr(java.lang.Exception ex) {
	printErr(ex.getMessage());
}


Back to JavaHowTo

Alumni Liaison

Questions/answers with a recent ECE grad

Ryne Rayburn