Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How can an applet read a serialized object residing in the same directory?

0
Posted

How can an applet read a serialized object residing in the same directory?

0

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

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123