Solved

beginning programming question

Posted on 2004-09-22
12
200 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 80
web services creation SOAP vs REST 5 56
java jdbc batch example 8 39
jsp login check 12 42
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

790 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