Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Persisting data from GUI application

 
Ashish Gupta
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ashish Gupta
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Premier Indore
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic