• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic Input/Output Question

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • 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
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for the detailed reply, Joe. I appreciate it, and I will check out those links you sent.
Landon
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!