Solved

beginning programming question

Posted on 2004-09-22
12
197 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
Comment Utility
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
Comment Utility
You could serialize the array using ObjectOutputStream, then use ObjectInputStream to read it back.
0
 
LVL 6

Expert Comment

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

Assisted Solution

by:MogalManic
MogalManic earned 31 total points
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:dwanders
Comment Utility
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
Comment Utility
With JDBC you can execute query (fundamentally).
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
0
 
LVL 12

Expert Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 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

12 Experts available now in Live!

Get 1:1 Help Now