Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

More Basic I/O Questions  RSS feed

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • 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
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • 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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!