Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert an input stream to a java object

Tags:

java

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(reg_be);
oos.flush();
oos.close();

InputStream is = new ByteArrayInputStream(baos.toByteArray());

This code convert Java Object to InputStream and how can I convert InputStream to an Object? I need to convert my Object to an InputStream then I pass it and I want to get my Object back.

like image 720
sabarirajan Avatar asked Jun 11 '13 11:06

sabarirajan


People also ask

How do you convert an input stream into String in java?

To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.

Can we convert InputStream to File in java?

Using nio packages exposed by Java 8, you can write an InputStream to a File using Files. copy() utility method.


2 Answers

In try block you should write:

ObjectInputStream ois = new ObjectInputStream(is);
Object object = ois.readObject();

ObjectInputStream is initialized with another stream, e.g. BufferedInputStream or your input stream is.

like image 186
darijan Avatar answered Oct 25 '22 00:10

darijan


ObjectInputStream ois = new ObjectInputStream(is);
Object object = ois.readObject();
like image 27
Buhake Sindi Avatar answered Oct 25 '22 02:10

Buhake Sindi