0

How would I go about searching a map that hasbeen written to a file?
My GUI has a method caled processFind() which prints all keys in the map, I have this working but not if the map is saved to a file.

private void processFind()
    {
        gui.results.setText("");
        for (Object s : customerDetails.keySet()) 
        {
              gui.results.append(s.toString().toUpperCase() + ": " + 
                                 customerDetails.get(s).toString().toUpperCase()
                                 + newline);
        }
    }

I also have a method that prints all key/value pairs that begin with a user defined string

 private void processSearchUser()
    {
        gui.results.setText("");
        String upper = gui.nameText.getText().toUpperCase();
        for(Object s : customerDetails.keySet())
        {
            if(s.toString().startsWith(upper))
            {
                gui.results.append(s.toString().toUpperCase() + ": " +
                                   customerDetails.get(s).toString().toUpperCase()
                                   + newline);
            }
        }
        if (gui.nameText.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Please enter part of a name to search");
        }
    }

Thank for looking
Glen..

3
Contributors
9
Replies
10
Views
4 Years
Discussion Span
Last Post by GlenRogers
0

Is the map too big to be read into memory (tens, even hundreds, of MegaBytes is OK)? If so you probably should be using a proper database to hold the data and do an SQL query. If you can just read it into memory then your existing code will do.

0

My existing code does not read from the file that I have saved the map to though.

Do I need to use a scanner object?
Thanks

0

We can't say how to read it because you have not told us how you wrote it to the file - writeObject? XMLEncoder? Print? etc?

Edited by JamesCherrill

0

Sorry, I should have had this with my previous topic about writing a map to file.

I wrote it like this.

 try
         {

             File file = new File("reboot_customer_details");  
             FileOutputStream f = new FileOutputStream(file);  
             ObjectOutputStream s = new ObjectOutputStream(f);          
             s.writeObject(customerDetails);
             s.close();
        }
         catch(Exception ex)
         {
             gui.results.setText("Error: " + ex);
         }
0

That's good - it makes it really easy. Just open an ObjectInputStream to the same file, and read your whole Map back in with a single readObject(). (You just need to cast the Object that you read back to TreeMap)

0

Sounds good James, but how do I cast an object to TreeMap?

I'm stuck after I done this

try
         {
             File file = new File("reboot_customer_details");  
             FileInputStream f = new FileInputStream(file);  
             ObjectInputStream s = new ObjectInputStream(f);          
             s.readObject();
             s.close();
        }
         catch(Exception ex)
         {
             gui.results.setText("Error: " + ex);
         }
0

To cast the reference, you just need to add the class name in parenthesis in front of the method call, ie.
MyClass obj = (MyClass)s.readObject();

You can read a bit more about the ObjectInputStream and see a couple of examples here.

Edited by Ezzaral: Updated link to 1.6 api

0

I'm still having trouble with this.
Heres my method that is supposed to read fromm the file of the previously saved map

private void processFind()
    {
        gui.results.setText("");
        Map map = new TreeMap<String, Customer>();


         try
         {
             File file = new File("reboot_customer_details.txt");  
             FileInputStream f = new FileInputStream(file);  
             ObjectInputStream s = new ObjectInputStream(f);          
             map = (TreeMap)s.readObject();
             s.close();
        }
         catch(Exception ex)
         {
             gui.results.setText("Error: " + ex);
         }

        for (Object s : map.keySet()) 
        {
              gui.results.append(s.toString().toUpperCase() + ": " + 
                                 map.get(s).toString().toUpperCase()
                                 + newline);
        }
    }

And this is the ouput it gives me
Error: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.TreeMap.

Where am I going wrong??
Thanks

0

I seem to have fixed this.

Where I write the map to file I changed this line

s.writeObject(customerDetails);

to this

s.writeUnshared(customerDetails);

This seems to have done the trick.

Glen..

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.