Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing Integer Object to disk

 
Blanka Tierrablanca
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to write an Integer Object to the disk and then read it back. I can't figure out the command for reading it back. I know it needs to be cast as appropriate wrapper class. Someone please help.
Thanks
File fd = new File("d:\\Number.dat");
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fd));
out.writeObject(new Integer(17));
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fd));
System.out.println( (in.readObject).intValue() );
}
catch (Exception e) {}
}
}
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cast it to Integer object like this

The following code will also work fine, since the toString() method in the Integer class returns the integer value
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!