• 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

More Basic I/O Questions

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(1)Can a program only read one object from a file? What if I have multiple objects serialized to one file, but I only want to read one or two of those objects? Is there a way to dictate which object gets serialized?
(2) When creating a file object, you include the file name enclosed in quotation marks as an argument to the constructor of the object. Is it possible to substitute a string variable with the name of the file as the string value, or do you have to use the file name itself?
Example:
String fileName= "testfile.dat"
File fileHolder = new File(String fileName)
(3) When writing data to a file, can you use a custom file extension, or do you have to use an existing file extension like .txt or .csv?
Thanks for all the help,
Landon
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(1) Multiple objects can be serialized into one file. For example, if you have 3 String objects serialized to a file, then you can do 3 readObject() calls to get each one sequentially.
(2) Absolutley. As far as creating that File object, there is no difference between these 2 lines of code:

(3) The extension can be anything you want. For the most part, extensions don't do any magic, they just give a hint as to what type of file they are . You could create a file called "myfile.foo.bar".
[ March 12, 2004: Message edited by: Blake Minghelli ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For fun, google for file extensions and mime types. You should be able to find a table of several hundred extensions and possible mappings to applications. Most of which I've never heard of. Extensions don't have to be 3 characters any more either. So you can certainly make up your own!
 
reply
    Bookmark Topic Watch Topic
  • New Topic