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..

Recommended Answers

All 9 Replies

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.

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

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?

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);
         }

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)

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);
         }

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.

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

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..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.