Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Java Project: Help with Objects and Files Needed  (Read 1547 times)

Frelock

  • Bay Watcher
  • Dabbling Philosopher
    • View Profile
Java Project: Help with Objects and Files Needed
« on: February 26, 2009, 06:14:46 pm »

I'm pretty much so a complete beginner when it comes to programming.  I've taken two classes on programming in the java language, and have a decent grasp on the ideas behind object-oriented programming.  Beyond the simplest of applications, however, I'm somewhat at a loss.  I need your help, Bay12 programmers.

Spoiler: The Project (click to show/hide)

The main question I currently have is: how do I save objects of a class in a permanent format (such as a file)?  How can I read said file and import objects from it?  How can I change said file without re-creating it?  Any help directly from you would be appreciated.  Pointing out some helpful (read: easy to understand with minimal background) tutorials or walk-throughs online would be awesome too.

Also, if you have any tips on implementation of the program, or anything in my description that you think could use some working on, I would be happy to have you point it out.  New ideas for the project could help too.  Eventually, I'd like to wrap it up in an .exe file, but that's a long way off (not to mention impossible for me with my current programming experience).
Logged
All generalizations are false....including this one.

Jamuk

  • Bay Watcher
    • View Profile
Re: Java Project: Help with Objects and Files Needed
« Reply #1 on: February 27, 2009, 08:05:24 pm »

http://java.sun.com/docs/books/tutorial/essential/io/
You should really use google, I found it in a couple minutes.  But, yeah, that should have more than enough info for you.  If you need more just search for it.  The APIs are free, and everyone can see them.

If you wanted to modify the contents of an existing file, the safest way to do it would be to parse the file contents and convert them into objects, then you can close the file and write to it safely.

Also, best way to do your project would likely be to make your own file syntax, there are several ways you can do this.
I would probably character values that aren't viewable to keep track of breaks between items, you can use a different one to break between each of the seperate properties included in that item.  Then it's just a matter of using string split operations which won't be too hard at all.
For example (psuedocode):

for each line in the file (One object per line, you can go more in depth easily though by using different characters or whatever)
-Split by character of value (20) <I think that's a non-displayable character, just an example though>
-Create new object based on the type of the first item, its parameters are the next few items.
Use interface to modify the items, you've loaded it now they get to play the game or whatever.
for each of your items
-Start a new line between each object
-Use characters of value(20) to seperate each necessary item for each type, make sure you use the same order or it won't work right

And ta-da! It'll be done!  I'd use a more complicated file system if this was a big project, but there aren't too many complex data structures involved in this, and I'm sure you could figure it out if you had to (better to learn anyways :).

Alright, does that answer your question or is there something else?  I've taken a few years of computer science and although I'm not an expert I should be able to help answer your questions as this doesn't seem like a horribly complex problem


-EDIT
Oh yeah, and the best way to make a large project is to do it in stages as you add complexity, for your project I might start out by making a program that can take user input and put it into a file.  Then make it so that if you give it a list of ',' seperated items it will change all the commas into another thing, then make an object that you assign values to by prompting the user, then try to get it so that that can be stored in a text file.
I probably wouldn't even mess with making it into a windows application until I got it all working right, as once you have the framework built it's just a matter of calling methods from the form.
« Last Edit: February 27, 2009, 08:17:20 pm by Jamuk »
Logged

Shades

  • Bay Watcher
    • View Profile
Re: Java Project: Help with Objects and Files Needed
« Reply #2 on: March 02, 2009, 04:14:09 am »

Can I suggest you check out junit and unittest your code, you'll find it much easier to change things confidently later on and it will probably be easier to learn before you get too many bad habits :)

The easiest way to do saving and loading would be via the Serializable interface then you just need to be able to break down and rebuild the class as Jamuk suggested. Making it Serializable will allow you to just pass the object to any of the java stream classes and then that can handle the mechanics.

This would not be easy to alter the file without writing the whole thing out again. (although personally I doubt you'll find that is a problem).

Personally I would use something like sqlite and saving your character data in the database when you need to save / load. However I'd probably end up using it while the game is running too as it's pretty fast and databases give you lots of free functionality :)
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd