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?

chuff
  • 5,846
  • 1
  • 21
  • 26
sabarirajan
  • 177
  • 2
  • 3
  • 13

2 Answers2

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

Community
  • 1
  • 1
Houser
  • 41
  • 6
  • 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? – sabarirajan Jun 12 '13 at 05:00
  • 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. – Houser Jun 12 '13 at 05:05
  • http://stackoverflow.com/questions/17057927/getting-error-while-receiving-object-in-restful-webservice – sabarirajan Jun 12 '13 at 05:07
0

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

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115