Question

I'm trying to allow the user to either select an already created .ser file and save over it, or create a new .ser file by typing in a new name in the JFileChooser textfield. As you can see from the code below, I used a if/else statement to determine which of the two the user is doing. The problem I'm experiencing is that no matter how I rearrange things, or use different if conditions, the JFileChooser always chooses the latter option (create a new .ser file by typing in a new name). This wouldn't be a big problem, but it always adds ".ser" to the file.

For example: If I create a new file in JFC called mySERObject, it will be saved as "mySERObject.ser." Now when I open JFC again, and I select with my mouse mySERObject.ser, to save over, it instead creates a new file called "mySERObject.ser.ser."

I uses the System.out.println to see which statement gets exected, and it's always the "First one printed." Here's my code:

    private void addSaveAsListener(JMenuItem item) {
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser fc = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "Serialized Object Files", "ser", ".ser");
                fc.setFileFilter(filter);
                final JTextField textField = getTextField(fc); //gets text from JFC textfield
                int returnVal = fc.showSaveDialog(null);
                String fileName = textField.getText();
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    if (!(fc.getSelectedFile().length() > 0)) {
                        System.out.println("first one printed");
                        File file = new File(fc.getCurrentDirectory(), fileName
                                + ".ser");
                        try {
                            file.createNewFile();
                            fileSystem.saveAs(addressbook.getCopyList(), file.getAbsolutePath()); //serializes arraylist
                        } catch (IOException e) {
                            JOptionPane.showMessageDialog(null,
                                    "File unable to be created.");
                        }

                    } else {
                        String path = fc.getSelectedFile().getAbsolutePath();
                        fileSystem.saveAs(addressbook.getCopyList(), path); //serializes arraylist
                        System.out.println("2nd one printed");
                    }
                }

            }
        });
    }

I was wondering if you could help me with what's wrong or by offering solutions, thank you.

Was it helpful?

Solution

Question 1: There is always another suffix added, what can I do?

Look at the following code from your example. You will see, that you get the textfield from the fc, then get the string from that (aka "mySERObject.ser") and then you gone save again, with ".ser" appendig. You can maybe use some String opperations on fileName to get rid of the suffix before further processing (for example with fileName.replace(".ser", "")).

final JTextField textField = getTextField(fc);
String fileName = textField.getText();
//fileName.replace(".ser", "")
File file = new File(fc.getCurrentDirectory(), fileName + ".ser");

Question 2: In my if/else block only if clause will be selected. Why?

I personally don't know much about JFileChooser, but fc.getSelectedFile().length() seems not to work like you think, since it always returns 0. But you can just use fileName.length(), can't you?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top