in this link there is my project, i create a java text editor for open docx documents. the text editor should do the basics functions of a text editor, as copy, paste , select all, delete, exit, new, open , save and save as. i tested the code and i have a problem only with the function "save", because the program don't do anything, but " save as" it run correctly. could someone help me to solve the problem. the part of the code that is about "save" is this:

  private void SaveActionPerformed(java.awt.event.ActionEvent evt) {                                     


        JFileChooser sdChooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Text file", "txt");
        int returnval = sdChooser.showOpenDialog(null);

            if(returnval == JFileChooser.APPROVE_OPTION){
            File directory = sdChooser.getCurrentDirectory();
               String path = directory.getAbsolutePath();
               String fileName = sdChooser.getSelectedFile().getName();

                   fileName = fileName + ".docx";    
               jTextArea.exportDocument(new FileOutputStream(new File(fileName)));
               //BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path + "\\" + fileName)));
        }catch(IOException e){      
    JOptionPane.showMessageDialog(null, "ERROR!");

Recommended Answers

All 3 Replies

You've defined the "Save" function to work only if it's the first time that the user's trying to save the doc and CurrentFileDirectory is not blank. I wager you forgot to add an else condition to that: what if the file HAS been saved and CurrentFileDirectory is not blank?

Basically, there is no else to the if at line 3.

i try to remove the function "save" for have only "save as.." but now i have an output error with " save as -_-'

There was no need of doing that, I guess. Send the cde for "save as", and let me check that! BTW you understood why save wasn't working, right?

Be a part of the DaniWeb community

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