• Post Reply 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

Writing Integer Object to disk

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) {}
}
}
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic