• 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

Basic Input/Output Question

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty new to Java, and I had a question about input/output.
What I'm trying to figure out is how to define a variable in a Java object when the value for that variable is stored in an external file. (Like a text file.)
For example:
I have a monster class in my program.It contains 3 variables.
(1)monsterSize (an int value)
(2)monsterColor (a String value)
(3)monsterHairy (a boolean value)
If I want to set the variables for a "cyclops" object of the monster class using the values of:
monsterSize = 3
monsterColor = Blue
monsterHairy = False
How would I do this? How would I format my text file? Would I have to use a .csv file?
Thanks for any tips or suggestions,
Landon
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Landon Blake:
I'm pretty new to Java, and I had a question about input/output.


There are about a dozen different ways to do this sort of thing. Your particular situation will make some solutions more useful than others (i.e. if a human has to read the stored file, a CSV or XML is better than Object Serialization).
Object Serialization turnes the state of a Java Object into a stream which can be written to file or sent across a network.
Random Access Files can store Java primitives, but it's up to you to determine the file format and write the code to distill the Java object's state and to recreate it.
Java IO Streams can be used to store the state of an object as a readable text file, a CSV or a binary file. Again, it is left to the programmer to write the code to read and write the object state.
XML is a popular solution to the problem of having a bunch of data in a text file and not knowing what it means. While Java provides XML parsers, it is still up to the programmer to create usable objects out of it.
The way I see it, if you are just storing a few object's states, serialization is the way to go. If you need to be able to read the stored states, or want to be able to use a file input to feed data to your program, some IO Stream (BufferedReader, for instance) may be the ticket. If other people will be specifying inputs, XML can be a good solution because you can specify a DTD (document type declaration) to enforce the format of the document. Random Access Files are a good choice if you are storing a bunch of data and need to read it from various places as a user interacts with a program. Standard engineering tradeoffs.
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the detailed reply, Joe. I appreciate it, and I will check out those links you sent.
Landon
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are properties files like "old-school" or something? I kinda like them as they are easy to maintain with an editor but don't see anyone recommend them for questions like this.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are properties files like "old-school" or something?
Well they have been partially displaced by Preferences, which are another option here. I still use Properties often enough, though I hate the way the store() method sorts everything by hash code. Sometimes I've just made my own custom class which wraps a LinkedHashMap and has simple methods to write and parse a Properties-like file format, but without losing the original order of things. Actually the main incentive when I did that was to be able to log (or throw an exception) whenever someone requested a property that didn't exist, since in my experience that often represents an error - someone misspelled something, or changed the property name. Once I had a separate class to wrap the Properties, it was easy to replace the Properties with a LinkedHashMap and make a nicer store() method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic