Solved

saving

Posted on 2004-08-14
33
209 Views
Last Modified: 2010-03-31
Hi experts,
I need some details for this answer since I have no idea how to go about it.
I want to add Save to my menu items and when pressed, 2 arrays get saved but Im not sure how this is done or if it will require a database. one is an array of names. the second is an array of numbers. I need to save both but be able to diffrentiate between them.

Please include examples and step by step instructions.
Thanks
0
Comment
Question by:Samooramad
  • 14
  • 9
  • 7
  • +1
33 Comments
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 11799126
you copuld simply store it to a file:

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(myarray1);
out.writeObject(myarray2);
out.close();
0
 

Author Comment

by:Samooramad
ID: 11799162
you mean a text file?
0
 
LVL 92

Expert Comment

by:objects
ID: 11799198
no that'll save it as a binary file, dop you need it as a text file?

0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 11799652
Just pass a file name and each array to this method and it'll save it as a text file:


      public void saveArray(String fileName, Object[] array) {
            PrintWriter out = null;
            try {
                  out = new PrintWriter(new FileOutputStream(fileName), true);
                  for (int i = 0; i < array.length; i++) {
                        out.println(array[i]);
                  }
            }
            catch (IOException e) {
                  e.printStackTrace();
            }
            finally {
                  if (out != null) {
                        out.close();
                  }
            }
      }
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 150 total points
ID: 11799797
Hi Samooramad,

Another solution is :

     public void save(String file, String[] names,int[] numbers)
     {
          PrintStream out=null;
          try
          {
               out=new PrintStream(new FileOutputStream(fileName));
               out.println(names.length);
               for (int i=0; i<names.length; i++) out.println(names[i]);
               out.println(numbers.length);
               for (int i=0; i<numbers.length; i++) out.println(numbers[i]);
          }
          catch (IOException e)
          {
               e.printStackTrace();
          }
          finally
          {
               if (out != null) out.close();
          }
     }


in all cases, in the file, you need to indicates the number of strings in the array in order to know where the array of number starts.
0
 

Author Comment

by:Samooramad
ID: 11800431
sorry object, I had to run. no I dont need it as a text file. I just asumed thats what it was. I hadnt even considered saving as a file. I thought I had to use a database
0
 

Author Comment

by:Samooramad
ID: 11800454
ok you guys need to advise me on what to do. because later I will need to add Open also to the menu items and I will have to read whatever I saved on the files and be able to read names seperately and numbers separetly. So I have no idea what would make this all easier
0
 

Author Comment

by:Samooramad
ID: 11800469
Webstrom, when I want to read from that file would I have to read all at once? or could I read certain names or numbers?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11800502
If the menu items refer to properties of a class, you could save it with an XMLEncoder. See example:

http://java.sun.com/j2se/1.5.0/docs/api/java/beans/XMLEncoder.html
0
 

Author Comment

by:Samooramad
ID: 11800525
no they arent
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 150 total points
ID: 11800550
>> Webstrom, when I want to read from that file would I have to read all at once? or could I read certain names or numbers?

It's like you want. If you want to read all at once, (1) you first rest the number of strings, (2) allocate the array, (3) read the strings (one per line), and then read the array of numbers as in (1) (2) (3).

Or you can read the number of strings, and then :
  - skip this number of lines if you want to read the array of numbers,
  - skip some lines in the file to access the line(s) you want

but if you have huge arrays of names and numbers, you may have to use binary file (less bytes used) and build an offset table to access directly some lines in the file, or use a database which can manage this kind of things.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11800585
>>no they arent

If you made them so, it would probably be more convenient
0
 

Author Comment

by:Samooramad
ID: 11800587
well at the moment Im not using loads of names.. but eventually I do want it to handle lots of name (for example all the names of students in different schools)
I dont know what an offset table is or how to make one (or how to use one in fact) so I dont know if that would be better than a database (which I also dont know how to use)
Any suggestions? :)
0
 

Author Comment

by:Samooramad
ID: 11800599
>>If you made them so, it would probably be more convenient
I think I would rather not at this point Cehj. It would take a lot of undoing
0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 11802159
To read the arrays back:

ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
Object[] myarray1 = (Object[]) in.readObject();
Object[] myarray2 = (Object[]) in.readObject();
in.close();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11804159
>>one is an array of names. the second is an array of numbers

How are these related?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Samooramad
ID: 11808416
they are both in different class but may be associated at certain times
0
 
LVL 92

Expert Comment

by:objects
ID: 11808428
The above code will handle saving/reading them.
0
 

Author Comment

by:Samooramad
ID: 11808681
objects would you mind explaining to me whether the two arrays are saved to one file or two? and how they are read separetly? or is that what your code does?
0
 

Author Comment

by:Samooramad
ID: 11808709
I mean does that code rerad the whole array at once or could I read array elements one at a time?
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 11808742
>>they are both in different class but may be associated at certain times

This association may be better encapsulated by a class in your program in that case as the relation should probably be modelled anyway. You could then operate directly on instances of that class.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 11808759
> objects would you mind explaining to me whether the two arrays are saved to one file or two?

that code saves them to same file, to save to two different files would just involve using different streams.

> and how they are read separetly? or is that what your code does?

Yes the code reads them seperately.

> I mean does that code rerad the whole array at once or could I read array elements one at a time?

It reads the whole array at once.
Unless the array is *huge* it most likely the simplest solution.
0
 
LVL 92

Expert Comment

by:objects
ID: 11808770
0
 

Author Comment

by:Samooramad
ID: 11808772
ok CEHJ but even if I do that I want to be able to do basically these things:
1- I need to save the array of names and the array of numbers
but I also need to be able to read the names seperatly from the numbers and also possibly read certain elements from either array.

I know my question is only about saving so I dont want to bother you guys with code for reading. I just thought it might have some significance as to how the arrays should be saved.. maybe Im wrong
0
 
LVL 92

Expert Comment

by:objects
ID: 11808787
> but I also need to be able to read the names seperatly from the numbers and also possibly
> read certain elements from either array.

Once you have read the arrays from files, you can access the elements however you need to.

> I just thought it might have some significance as to how the arrays should be saved.. maybe Im wrong

not really.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11808788
It's difficult to comment more without knowing exactly what you're doing. What do these arrays represent?
0
 

Author Comment

by:Samooramad
ID: 11808790
objects, could a class be saved the same way? with all its attributes?
0
 
LVL 92

Expert Comment

by:objects
ID: 11808797
> objects, could a class be saved the same way? with all its attributes?

yes, the same code can be used.
0
 

Author Comment

by:Samooramad
ID: 11808811
Its ok CEHJ.. Im confused myself
I think objects code will work.. if not you will probably see another question posted soon enough. then I will post my code.
thanks for putting so much effort in to understanding my problem :)
0
 

Author Comment

by:Samooramad
ID: 11808814
thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11808880
8-)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 11810405
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 11816030
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

705 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

13 Experts available now in Live!

Get 1:1 Help Now