This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Learning Java, storing data permanently? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Learning Java, storing data permanently?" Watch "Learning Java, storing data permanently?" New topic
Author

Learning Java, storing data permanently?

Ish Del
Greenhorn

Joined: Dec 05, 2012
Posts: 18
Hello all, so I am plugging away learning the Java language better and more OO concepts. I feel i have a nice general knowledge of java to draw from but had a question about writing a program that i could close and save data for next time i run it. Most of the examples you get in the books are programs that are meant to solve a problem and then move on to the next one. I wanted to ask what about a program that keeps an inventory for example without knowing anything about databases can I use file input output for this?
So lets say i wanted to create a program that kept an inventory of things in my fridge. Ill research and figure out how to structure and what classes to use so my question to you is:

1) Can a file be used to save this info? Is there any other way to save this? How would you go about saving this? Can an array or map or whatever other type of collection i choose be stored in a file?

2) In the real world is this always done with a database?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39090
    
  23
A database is probably the best way to do that, but you can also serialise an object and store it in a file.
You can write the details of an object in an XML file (as done by beans) or you can write your details into a file (text or binary) and read them back when you reopen the app.
Ish Del
Greenhorn

Joined: Dec 05, 2012
Posts: 18
Campbell Ritchie wrote:A database is probably the best way to do that, but you can also serialise an object and store it in a file.
You can write the details of an object in an XML file (as done by beans) or you can write your details into a file (text or binary) and read them back when you reopen the app.


Thank you. I will do some google searches on how to serialize my inventory object to see if i can store it into a file. I don't actually know anything about xml so i will look into that as well.

That might be a good way to do it, i can write when im done and import upon starting the program again that way the program stays in memory the whole time. Versus reading and writing via a method every time i look for or add an item.


Thank You again.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

You can also introduce an abstraction layer for persistence in the form of DAO interfaces. This will allow you to hide the details of how your objects are persisted. Your initial implementation might be to serialize your objects to files, then you might switch to XML, then eventually wise up and just use a database. The rest of your program should not have to change when you change your persistence mechanism and a DAO layer allows you to achieve this.

BTW, DAO is short for Data Access Object.


Junilu - [How to Ask Questions] [How to Answer Questions]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Ish Del wrote:That might be a good way to do it...

Just to elaborate a little on what Campbell and Junilu said: the important thing is that you keep what you want to persist (presumably a Java object, or set of objects) completely separate from how you're going to persist it.

It might even be worth writing a Persistence interface containing methods that allow you to read and write the objects you want without deciding how they are going to be stored (this is what Junilu was talking about with his "persistence layer").

Unfortunately, different objects, by their nature, often require different ways or reading and writing (still detached from how that is done), which is where the DAO part comes in. If you like, it's a specialized persistence interface - usually tied to a specific Java object - but it still shouldn't care whether its being written to a file, a database, or to a network port to be picked up by another program.

Hope I haven't muddied the waters for you.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Learning Java, storing data permanently?