aspose file tools*
The moose likes Beginning Java and the fly likes Please help me debug this - Converting Object to byte[] and vice versa. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

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

Sreenivasan Padmanabhan
Greenhorn

Joined: Apr 11, 2007
Posts: 4
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

Next time you'll think twice before writing "// ignore" in a catch block
Try "e.printStackTrace()" instead and you'll get the answer.


[My Blog]
All roads lead to JavaRanch
Sreenivasan Padmanabhan
Greenhorn

Joined: Apr 11, 2007
Posts: 4
Thank you very much!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

please don't double post. I am closing this thread. please make all follow-ups in the other thread.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help me debug this - Converting Object to byte[] and vice versa.