Java Question about writing an object to a file.

Discussion in 'OT Technology' started by Ben Fong-Torres, Dec 7, 2003.

  1. For a project I have to make a database program using Java. When the user clicks on the save button in my gui I get the name the user wants the file to be, create a new ObjectOutputStream with a new FileOutputStream with the supplied file name. Then I call the writeObject method on the ObjectOutputStream and pass it the database class I have which contains a HashMap and another map that extends HashMap. The problem I have is that I simply cannot get any file to show up in the directory. Here is my code:
    Code:
    if (source==Save){
                String fn=JOptionPane.showInputDialog("Please enter the name that you wish to save the database as.");
                fn.trim();
                try {
                    oos=new ObjectOutputStream(new FileOutputStream(fn));
                } catch (Exception e){
                    JOptionPane.showMessageDialog(null, "An error occurred while trying to create the ObjectOutputStream.");
                }
                try {
                    oos.writeObject(cdc);
                } catch (Exception e){
                    JOptionPane.showMessageDialog(null, "An error occurred while trying to write the object to the file.");
                }
                try {
                    oos.close();
                } catch (Exception e){
                    JOptionPane.showMessageDialog(null, e);
                }
            }
    }
    
     

Share This Page