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?
 
Ravindra76Connect With a Mentor Commented:
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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
All Courses

From novice to tech pro — start learning today.