Re: StreamCorruptedException throw by ObjectInputStream created from FileInputStream
On Jul 24, 2:24 am, Flavio Castelli <mic...@bglug.it> wrote:
I, I'm trying to load an object serialized to a file using an
ObjectOutputStream.
My code looks like this one:
1. FileInputStream fis = new FileInputStream(pwdFileName);
2. ObjectInputStream keyFile = new ObjectInputStream(fis);
3. SecretKey deskey = (SecretKey) keyFile.readObject();
4. keyFile.close();
I get a StreamCorruptedException at line 3.
Debugging I discovered that FileInputStream::available() return 258 bytes,
while FileInputStream::available() returns 0.
[snip exception]
This code is called from the method contextInitialized of a class
implementing ServletContextListener.
If I use the code into a java command-line program everything works fine.
Any hint?
Thanks in advance
Flavio
It could be that you aren't opening the file you think you are. Try
printing or logging "new File(pwdFileName).getCanonicalPath()", that
should tell you exactly which file you are opening. Generally you're
better off putting resources like this in your classpath, and using
ClassLoader.getResource(pwdFileName); the getResource method will
search all of your classpath and return the URL of the file.