I have an array of person class
Class Person
{}
Could anyone please help me to convert Person[]
array to inputstream or to a byteStream?
Thanks in advance.
I have an array of person class
Class Person
{}
Could anyone please help me to convert Person[]
array to inputstream or to a byteStream?
Thanks in advance.
To be able to write (serialise) your objects to streams, your class should implement Serializable interface. In most cases you don't need to do anything except for adding the "implements Serializable" clause in your class definition:
class Person implements Serializable {
// your class's fields and methods
}
Then, of course, you are not writing to the input stream, but to an output stream:
Person p = new Person();
// some more code here...
OutputStream os = new FileOutputStream("persons.txt"); // open file as a stream
os.write(person); // write person object to the stream
os.close(); // close the stream
To convert to a byte array you will have to use serialization still:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(person);
byte[] bytes = baos.toByteArray();