Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

Please help me debug this - Converting Object to byte[] and vice versa.

 
Sreenivasan Padmanabhan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Report post to moderator
Can someone tell me what is the problem here. When I convert an Object into byte[] and then convert it back to an object, I dont get the proper output.
This is the output I get:
107
[B@f276af2

Thanks,
Sreenivasan

import java.io.*;

public class temp
{
public static void main(String args[])
{
help obj = new help();
obj.a = 10;
obj.b = 'a';
System.out.println(obj.a + obj.b);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
oos.close();
bos.close();
byte[] b = new byte[bos.size()];
b = bos.toByteArray();

final ByteArrayInputStream ios = new ByteArrayInputStream(b);
ObjectInputStream os = new ObjectInputStream(ios);
Object x;
help x1 = new help();
x = os.readObject();
x1 = (help)x;
System.out.print(x1.a + x1.b);
}catch (Exception e) {
// ignore
}
System.out.println(new byte[0]);
}
}

class help
{
public int a;
public char b;
}
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
Next time you'll think twice before writing "// ignore" in a catch block
Try "e.printStackTrace()" instead and you'll get the answer.
 
Sreenivasan Padmanabhan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thank you very much!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12090
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
please don't double post. I am closing this thread. please make all follow-ups in the other thread.
 
    Bookmark Topic Watch Topic
  • New Topic