i have a method that saves an object to file :

public void Save()
        if (canSave)
         JFileChooser fileChooser = new JFileChooser();
           if (fileChooser.showSaveDialog (this) == JFileChooser.APPROVE_OPTION)
            ObjectOutputStream out;
            try {
                 out = new ObjectOutputStream (new FileOutputStream    (fileChooser.getSelectedFile ()));
                 out.close ();  
            catch (IOException ex) {  }
            JOptionPane.showMessageDialog(null, fileChooser.getSelectedFile(),
                    "game saved",JOptionPane.INFORMATION_MESSAGE);
         canSave = false;
          JOptionPane.showMessageDialog(null,"cannot save game","save error",

how can i make it save files with an extension? for example: s.myfile
and the same question about opening files with extension.

Recommended Answers

All 3 Replies

From what I understand, all objects are saved as .dat files for Java to reinterpret back into an object during deserialization.

To save files with an extension, I'd assume you'd use FileOutputStream and specify the kind of File you'd like to save.

If you mean to save an Object into a file other than the what's provided by Java, you will most likely have to do a 2-in-1 process of saving the Object as .dat file then opening a new file with the desired extension (Click) then opening a Stream to write bytes to that file, in which you'd have to read bytes from your Object file first then write those bytes to the desired file.

You could save yourself time by making a copy of the object file and attempting to change the extension manually, though I'm not sure if this is entirely recommended (or safe). It may be best to use a program that does this for you - one that Java provides most likely.

using JFileChooser you can set the type of file that the chooser will open or save to by using a filter HERE the internal link in the JFileChooser tutorial

although i haven't used it, i think that it will serve the purpose

ok i will try. thanks

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.