How can an applet read a serialized object residing in the same directory?
Location: http://www.jguru.com/faq/view.jsp?EID=118958 Created: Aug 4, 2000 Modified: 2000-08-04 09:31:07.376 Author: Jon Wingfield (http://www.jguru.com/guru/viewbio.jsp?EID=41079) Question originally posed by amol sinha (http://www.jguru.com/guru/viewbio.jsp?EID=84027 Normally when de-serializing from a file one would use a FileInputStream. However, in the case of an untrusted applet you can use an InputStream derived from the applet’s document base URL. To load a file “myClass.ser” residing in the same directory use: try { InputStream is = new URL(getDocumentBase(), “myClass.ser”).openStream(); ObjectInputStream ois = new ObjectInputStream(is); Object o = ois.readObject(); System.out.println(o); ois.close(); } catch (ClassNotFoundException cnfe) { System.out.println(cnfe); } catch (IOException ioe) { System.out.println(ioe); } A more general mechanism would be to use the static getResourceAsStream(…) method of ClassLoader. This searches for the resource along the application class