Solved

beginning programming question

Posted on 2004-09-22
12
198 Views
Last Modified: 2010-03-31
I am beginning to play with Java and the first application I would like to build will require storeing data inbetween program runs. I figure I could write to a text file, but I was wondering if there might be a better way to approach it? My plan is to have stored data that is read into an array or list at program start up then used while in the program. When I exit the program the data (and any new data added) is written back to the storage area.  No being very familure with Java and wanting to learn, I am really looking more for guidance than anything.

Thanks
0
Comment
Question by:dwanders
12 Comments
 
LVL 4

Assisted Solution

by:aratani
aratani earned 31 total points
ID: 12129782
Well, if the data you are going to write is simple then you should use a text file with the data and read the data and parse the file on start up.

However, if your data is too complex you could use object serialization to write the object (i.e. the array or the list) directly to the file and read it from there again. In this way, you won't need to parse the file and you could load the object directly from the file.

If you want to store the data, you have to use the files. If you want to understand the format then you have to decide how to write it to the file, and then read it from there. Otherwise, if you don't want to understand how the data is written and just read the data again from the file (without worrying about the structure of the data or the list), then you could use serialization.

You could just use the examples provided here and use it, it is pretty simple,

http://java.sun.com/docs/books/tutorial/essential/io/serialization.html
http://java.sun.com/docs/books/tutorial/essential/io/serializing.html

It is one of the nice features of Java. Hope you'll be on your way,

AJ
0
 
LVL 92

Assisted Solution

by:objects
objects earned 31 total points
ID: 12129786
You could serialize the array using ObjectOutputStream, then use ObjectInputStream to read it back.
0
 
LVL 6

Expert Comment

by:CodingExperts
ID: 12129967
Can you please elaborate the exact problem more.
Thanks
CodingExperts
0
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 31 total points
ID: 12129977
Look into XMLEncoder and XMLDecoder.  They can be used to serialize and deserialize Beans as XML files.  Something like this:
      Person[] people = {new Person(),new Person()};
      try {
            final String fileName="c:/temp/test.xml";
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
            XMLEncoder r = new XMLEncoder(out);
            
            
            r.writeObject(people);
            r.close();
      } catch (IOException e) {
            
            e.printStackTrace();
      }
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12130504
You can even use a Database to store the data, so to store and retrieve you can use the JDBC. In this manner you do not worry about serialization, etc. Only save and retrieve (the concurrence could be solved by Database itself; if you use file to save and restore, the concurrent access must be solved by your code).

Bye, Giant.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:dwanders
ID: 12132511
I will look into the above (serialization etc...), that database approch sounds interesting - In Visual Basic, I have been told it is pretty simple to add an Access database to a program as you data source - would this JDBC be the equivalent of that? If so - where can I find information about writting an app that has a small database (that is accessed only by the single program user - that gets packaged with the program itself) as a datasource?
0
 
LVL 12

Accepted Solution

by:
Giant2 earned 32 total points
ID: 12132542
With JDBC you can execute query (fundamentally).
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12132551
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12132560
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12132583
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12408032
I think a split could be done.
The asker give us no answer about what we suggest.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 128
Strange loading of website behaviour 3 32
sites similar to codingbat to improve coding hanson skills 3 41
simple java question 3 44
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now