wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Persisting data from GUI application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Persisting data from GUI application" Watch "Persisting data from GUI application" New topic

Persisting data from GUI application

Ashish Gupta
Ranch Hand

Joined: Apr 27, 2003
Posts: 61
Hello all,
I am new to GUI/Swing and am creating a simple application in Swing. As a part of my application, I want to persist the data, entered from the application. Currently I am using text file to save data using delimiters. I do not want to use any database, like oracle, mysql, ms access etc, in order to keep it simple.
In this regard I have 2 questions:
1. Is writing data to and reading from a delimited flat files the only way? Updating data is a hassle.
2. Can XML be used to make things (write/update/delete records) easier? If yes, could you please send/direct me to sample code.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299

Is writing data to and reading from a delimited flat files the only way? Updating data is a hassle.
No, it is not the only way.
Can XML be used to make things (write/update/delete records) easier?
If you don't know how to read/write to/from an XML document, initially, this can be difficult. In the long wrong it would prove to be better (maybe). I really don't think it would be any easier than a flat file. It would just be more robust.
Depending on what you are persisting, the easiest way is to use the Properties object. It will allow you to read/write key/value pairs like:
name=gregg bolinger
If this would work for you let me know and I will show you how it can be done using an InputStream to read the file into the properties object. Really easy stuff...

GenRocket - Experts at Building Test Data
Ashish Gupta
Ranch Hand

Joined: Apr 27, 2003
Posts: 61
Thanks Greeg,
I guess it will work fine for me. My file will have format more or less like
name=gregg bolinger
name=ashish gupta
location=bay area
which I am currently storing in flat file as
gregg bolinger^29^wichita
ashish gupta^30^bay area
I haven't have much coding exposure in XML but will not be too difficult to pick.
I will really appreciate if you can give some pointers.
Brian Pipa
Ranch Hand

Joined: Sep 29, 2003
Posts: 299
Properties will work for you but you'll have to think about how to store the data. Properties objects cannot have duplicate keys. You can do it a number of ways. You could do something like:

or you could do something like:

There are a number of ways you can do it. Take a look at the JavaDocs for Properties.
I also stumbled upon this the other day too: http://ostermiller.org/utils/doc/com/Ostermiller/util/UberProperties.html
You may be able to use that instead - I haven't tried it myself though. I think I remember reading that it can have duplicate keys.

My Java Freeware:<br />MACCC - <a href="http://maccc.pipasoft.com" target="_blank" rel="nofollow">http://maccc.pipasoft.com</a><br />Nator - <a href="http://nator.pipasoft.com" target="_blank" rel="nofollow">http://nator.pipasoft.com</a>
Premier Indore

Joined: Sep 12, 2003
Posts: 2
You can use XMLEncoder,Decoder classes in the package java.beans with JDK1.4.1. This provides very easy way to persist the data using the XML, but one constraints is that your class structure must follow the Java Beans architectute i.e. all the class members must have there getter/setters.

Hope this suggestion help you.
I agree. Here's the link: http://aspose.com/file-tools
subject: Persisting data from GUI application
Similar Threads
How to open an PDF file from Java Application
Netbeans JTable Binding - Retrieve data from selected rows
How to extend a swing application
Regular updates to table from network messages
re:How to use hyperlink in applet