• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing Integer Object to disk  RSS feed

 
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!