Solved

beginning programming question

Posted on 2004-09-22
12
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
xampp tool 12 60
hibernate example for saving data 19 77
Running JavaFX on JDeveloper 12C 1 78
spring maven example issues 3 44
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

726 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