Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thank you very much!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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