Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

saving

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
Samooramad
Asked:
Samooramad
  • 14
  • 9
  • 7
  • +1
7 Solutions
 
objectsCommented:
you copuld simply store it to a file:

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
out.writeObject(myarray1);
out.writeObject(myarray2);
out.close();
0
 
SamooramadAuthor Commented:
you mean a text file?
0
 
objectsCommented:
no that'll save it as a binary file, dop you need it as a text file?

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
CEHJCommented:
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
 
WebstormCommented:
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
 
SamooramadAuthor Commented:
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
 
SamooramadAuthor Commented:
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
 
SamooramadAuthor Commented:
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
 
CEHJCommented:
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
 
SamooramadAuthor Commented:
no they arent
0
 
WebstormCommented:
>> 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
 
CEHJCommented:
>>no they arent

If you made them so, it would probably be more convenient
0
 
SamooramadAuthor Commented:
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
 
SamooramadAuthor Commented:
>>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
 
objectsCommented:
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
 
CEHJCommented:
>>one is an array of names. the second is an array of numbers

How are these related?
0
 
SamooramadAuthor Commented:
they are both in different class but may be associated at certain times
0
 
objectsCommented:
The above code will handle saving/reading them.
0
 
SamooramadAuthor Commented:
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
 
SamooramadAuthor Commented:
I mean does that code rerad the whole array at once or could I read array elements one at a time?
0
 
CEHJCommented:
>>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
 
objectsCommented:
> 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
 
objectsCommented:
0
 
SamooramadAuthor Commented:
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
 
objectsCommented:
> 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
 
CEHJCommented:
It's difficult to comment more without knowing exactly what you're doing. What do these arrays represent?
0
 
SamooramadAuthor Commented:
objects, could a class be saved the same way? with all its attributes?
0
 
objectsCommented:
> objects, could a class be saved the same way? with all its attributes?

yes, the same code can be used.
0
 
SamooramadAuthor Commented:
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
 
SamooramadAuthor Commented:
thanks
0
 
CEHJCommented:
8-)
0
 
WebstormCommented:
:-)
0
 
objectsCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 14
  • 9
  • 7
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now