1

If I write my code like this, it gives an error "File not found access denied ......"

public class ImplRegistration implements IRegistration {
 @Override
    public boolean newRegistration(Registration_BE reg_be) {
        FileOutputStream fos = new FileOutputStream("serial.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(reg_be);
    }
}

For security reasons, I changed the fourth line of the code to this:

FileOutputStream fos = new FileOutputStream("f://serial.ser");

But then it showed the exception java.io.NotSerializableException: java.io.ByteArrayInputStream.

How can I serialize the object?

10
  • 3
    Make sure your object to serialize implements the Serializable interface. Also, it would be good if you share the problematic code and post the stacktrace instead of the error message. Commented Jun 12, 2013 at 4:09
  • i using windows 8 so even i give c:// also i showing access denied only. Commented Jun 12, 2013 at 4:09
  • 2
    C:/ will always give you access denied. Note that in Windows, the path should be like f:\\\\some\\path, or again, use /some/path. Commented Jun 12, 2013 at 4:10
  • 3
    Apparently your Registration_BE class includes a non-transient ByteArrayInputStream field. This is not serializable, so the entire class is not serializable. Commented Jun 12, 2013 at 4:12
  • 1
    really thank you Ted Hopp..i want this reason only y it is not serialize.thank you.. and is there any option to change my object as a inputstream with tat inputstream field. Commented Jun 12, 2013 at 4:16

2 Answers 2

1

The serialization operation in this case is failing because, as Ted Hopp states in the comments above, the class you are attempting to serialize contains a non-transient (and non-serializable) ByteArrayInputStream object. To remedy this and make the Registration_BE class serializable, you can mark this field as transient:

class Registration_BE {
  // rest of class

  private transient ByteArrayInputStream bais = null;

  // rest of class
}

This will cause it to be omitted from the serialization process of Registration_BE, but will also cause it to be uninitialized when the object is deserialized on the other end.

If you wish to initialize the ByteArrayInputStream after deserialization, you may want to consider writing custom writeObject / readObject methods for the Registration_BE class. There are many tutorials on custom serialization available on Google. The information in this thread might help to get you started:

Uses of readObject/writeObject in Serialization

Sign up to request clarification or add additional context in comments.

3 Comments

Kotz thank you. as ted stated now i cant receive inputstream in web service, So i remove tat field in my class. but still its not wroking Why?
Please be more specific than "it's still not working". Is it producing a new error message after you removed the field? If so, please post it, preferably with a stacktrace. Perhaps post the code for your class as well if possible.
0

If you try to access file in your operation system(OS) partition, it will give access denied error.

1 Comment

this is not relevant, look at the comments above, the issue was the ByteArrayInputStream was not serialisable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.