File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Persisting data from GUI application" Watch "Persisting data from GUI application" New topic
Author

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.
Thanks
Ashish
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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
age=29
location=wichita
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
age=29
location=wichita
name=ashish gupta
age=30
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.
Thanks
Ashish
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.
Brian


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
Greenhorn

Joined: Sep 12, 2003
Posts: 2
Hi,
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.
Regards
premier
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Persisting data from GUI application