开发者

Is there a better way... (Java, saving data)

开发者 https://www.devze.com 2023-04-06 04:04 出处:网络
I currently wrote a small program for a manager at work. It inputs an invoice number and creates that into a button, going down in a row. Then inputs a date of approval and creates that into a label(o

I currently wrote a small program for a manager at work. It inputs an invoice number and creates that into a button, going down in a row. Then inputs a date of approval and creates that into a label(on line with the created button). Lastly, creates a label for the date of expire(30 days later)(and again, online with the button created).

It saves this data into 3 files(1 for the button, 1 for the first label and one for the second label). Right now I'm using "ObjectOutputStream" to save the lists of buttons/labels. When the program is opened it re-loads all the data from the fi开发者_StackOverflow社区les and "re-prints" them onto the screen. The program also gives the user the option of deleting ROs in which case I re-write the data files minus the removed RO.

I am just wondering if there is a better way to do all this and if I'm wasting my time using this approach.


I'd favor a more reuseable data format over Java serialization when the goal is saving data for long term.

Serialization is great for sending complete objects over network or quickly/temporarily saving complete objects on disk while JVM is to be restarted or temporarily needs more memory and so on. However, it is not so great for saving data on long term. When you change/update your classes holding the data, the existing data will be unusable.

Consider XML (in combination with JAXB) or JSON (in combination with Gson) or an embedded SQL database (in combination with JDBC). This allows you easier and more fine grained control over backwards compatibility and also reusability by other programs/languages.


You should be saving data, not components. Can you construct the buttons, labels, etc on load and only save invoice numbers, date of approval, and date of expiry?

Also, you could store all data in an arraylist or hashmap and only save that object, rather than 1000's of smaller objects.

0

精彩评论

暂无评论...
验证码 换一张
取 消