Java project - Vectors and writing to disk

I am writing a notebook program, with 2 fields, note and keyword, stored in a vector.  I need to know
a) how to output it to disk, ObjectOutputStream?
b) How to link two items in Vector together to automatically display it in the text fields.

Can you help?
LVL 12
mmc98dl1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vladi21Commented:
a) better use traditional methods
http://tactika.com/realhome/javaht/java-f1.html
JavaTM I/O Performance Tuning
http://www.sun.com/workshop/java/wp-javaio/
Tuning JavaTM I/O Performance
http://developer.javasoft.com/developer/technicalArticles/Programming/PerfTuning/index.html


b) java.util.Hastable?
0
mmc98dl1Author Commented:
Adjusted points to 100
0
jjdunneCommented:
You could link the two items in the Vector together by using a Class and adding that Class to the Vector.

For example:

public class NotebookEntry {
     public String note;
     public String keyword;

     public String toString() {
           return keyword + " : " + note;
     }
}

.....

NotebookEntry n = new NoteboodEntry();
n.note = "This is the note.";
n.keyword = "keyword";

System.out.println("Entry = "+n.toString());


The other solution is to use a Hashtable, if you need to access the data via the keyword.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Ravindra76Commented:
Hi,

Here is a sample code for IO operations on vector of object.


import java.util.*;
import java.io.*;

public class NotebookOps {
      Vector vector;

      public NotebookOps () {

            writeToFile();
            String key="SE2";
            String note = readFromFile(key);
            System.out.println("NOte is:"+note);


      }
      public void writeToFile() {
            try {
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("note.txt"));
            Notebook n1 = new Notebook("SE1","ravindra");
            Notebook n2 = new Notebook("SE2","heyhey");
            vector.addElement((Notebook) n1);
            vector.addElement((Notebook) n2);
            out.writeObject((Vector) vector);
      

            }
            catch ( Exception ee) {}
      }
      public String readFromFile(String key) {
            String note="";
            try {
                  ObjectInputStream in = new ObjectInputStream(new FileInputStream("note.txt"));
                  Vector vector = (Vector) in.readObject();
                  for ( int i=0; i < vector.size(); i++ )
                  {
                        Notebook n1 = (Notebook)vector.elementAt(i);
                        if ( n1.key.equalsIgnoreCase(key)) {
                              note = n1.note;
                              break;
                        }
                              
                  
                  }
                  
            }
            catch ( Exception ee) {

            }
            
            return key;
      }
      public static void main (String args[]) {
            NotebookOps ops = new NotebookOps();
      }
}


Functionality:

1. It will store objects into file which is a vector of notebooks

2.It will get particular note given a key value.


What you have to do:

  I am returning note.
  You can set these values to textfiles
as

txt1.setText(n1.key);
txt2.setText(n1.note);

in readFromFile method.

BEst of luck

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shenmingCommented:
b) How to link two items in Vector together to automatically display it in the text fields.

My Suggestion: If your platform allows, you can try to use java2's java.util.Map class to store your keyword & note.
This class will map a key to a value. In you case, it will map the keyword to the notes. You can store this pair as one entry in the  map. When you want to retreive a pair, you just need to supply the keyword.

0
shenmingCommented:
> b) How to link two items in Vector together to automatically > display it in the text fields.

My Suggestion: If your platform allows, you can try to use java2's java.util.Map class to store your keyword & note.
This class will map a key to a value. In you case, it will map the keyword to the notes. You can store this pair as one entry in the  map. When you want to retreive a pair, you just need to supply the keyword.

0
shenmingCommented:
> b) How to link two items in Vector together to automatically > display it in the text fields.

My Suggestion: If your platform allows, you can try to use java2's java.util.Map class to store your keyword & note.
This class will map a key to a value. In you case, it will map the keyword to the notes. You can store this pair as one entry in the  map. When you want to retreive a pair, you just need to supply the keyword.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.