Results 1 to 11 of 11
  1. #1
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Question [SOLVED] jfilechooser for saving a file....?

    Hi Members,

    Im using jfilechooser to save a file, when i click a Save As button, a save as dialog pops up and if i give a file name that is already existing in my directory; it throws an error dialog, 'File already exists, do you want to replace it?' and if i click YES the file is replaced, but when i click NO again the save as dialog should be poped up and the validation should be continued for number of times the functuion is called. but for me only for the first time the valaidation is done and for the second time, if i give the same file name that already exist, it is not throwing any error dialog and simply it saves.

    Please help me out in this.

    here is my code below:

    Here is my code:

    CODE 1:

    private void btnSaveAsActionPerformed(java.awt.event.ActionEven t evt)
    {
    try
    {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory( new File( "./") );
    if ( chooser.showSaveDialog( this ) == JFileChooser.APPROVE_OPTION )

    {
    File fileName = new File( chooser.getSelectedFile( ) + ".log" );
    BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
    outFile.write( getTxtArea_log().getText( ) ); //put in textfile
    outFile.flush( ); // redundant, done by close()
    outFile.close( );
    }
    }


    But i changed it as:

    CODE 2:

    try
    {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory( new File( "./") );
    int actionDialog = chooser.showSaveDialog(this);
    if ( actionDialog == JFileChooser.APPROVE_OPTION )
    {
    File fileName = new File( chooser.getSelectedFile( ) + ".log" );
    if(fileName != null)
    {
    if(fileName.exists())
    {
    actionDialog = JOptionPane.showConfirmDialog(this, "Replace existing file?");
    if (actionDialog == JOptionPane.NO_OPTION)
    {
    chooser.showSaveDialog(this);
    BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
    outFile.write( getTxtArea_log().getText( ) ); //put in textfile
    outFile.flush( ); // redundant, done by close()
    outFile.close( );
    }
    if(actionDialog == JOptionPane.YES_OPTION)
    {
    BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
    outFile.write( getTxtArea_log().getText( ) ); //put in textfile
    outFile.flush( ); // redundant, done by close()
    outFile.close( );
    }
    return;
    //AttestDialog.getInstance( ).showErrorDialog(languageBundle.getString("LogFil eAlreadyExists"));
    }

    BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
    outFile.write( getTxtArea_log().getText( ) ); //put in textfile
    outFile.flush( ); // redundant, done by close()
    outFile.close( );

    }
    }
    }

    And in my code 2, it is working fine, but when i click save as button, save as dialog pops up and if i give an already existing file name, it throws an error dialog as "File already exists, do you want to replace?' and if i click CANCEL, the save as dialog again pops up and if i give the same file name which is already existing, it saves, without giving error dialog, that 'File name already exist?'

    I do not know why it happens,

    Looking forward for your help in this ROB.

    NOTE: Everything works fine for the first time, if i give the file name to save which is already exists, it throws an error dialog, and ask for 'File exist do you want to replace?' and if i click YES it replaces it; but if i click NO, the save as dialog opens again and again if i give the same name which is already exist, it is not throwing any error message, it simply replaces it with the new one.

    Question: For me error dialog should be thrown for all the time when i give a file name that already exists.?

    Please provide me some sample code for my requirement......?

    Regards,
    Prabhu.
    Last edited by prabhurangan; 06-17-2008 at 07:05 AM. Reason: Added some more details

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You don't have validate the file is exist or not even after the no options is selected. Just think about the logic there.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory( new File( "./") );
    int actionDialog = chooser.showSaveDialog(this);
    if ( actionDialog == JFileChooser.APPROVE_OPTION )
    {
        File fileName = new File( chooser.getSelectedFile( ) + ".log" );
        if(fileName == null)
            return;
        if(fileName.exists())
        {
            actionDialog = JOptionPane.showConfirmDialog(this,
                               "Replace existing file?");
            // may need to check for cancel option as well
            if (actionDialog == JOptionPane.NO_OPTION)
                return;
        }
        // okay to write file
        BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
        outFile.write( getTxtArea_log().getText( ) ); //put in textfile
        outFile.flush( ); // redundant, done by close()
        outFile.close( );
        //AttestDialog.getInstance( ).showErrorDialog(languageBundle.getString(
                           "LogFil eAlreadyExists"));
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default

    Hi Member,

    This was the one which im doing, but my project manager says that he needs the save as dialog again, once the user clicks NO option for showConfirmDialog, is it possible to do so? The saveAs dialogshould be always shown when the NO option is clicked for confirmDialog?

    Regards,
    Prabhu.

  6. #6
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default

    Hi Eranga,

    Im not getting of what you ask for. But this is the logic im in need of. each time when a save as dialog is opened, it should check for File exists, and if NO option is clicked again the dialog should appear and when save button is clicked it should check for File already exists? dialog.

    Looking forward for your reply.

    Regards,
    Prabhu.

  7. #7
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default

    Hi Eranga,

    Please i need your help in this, issue too.
    I could not fix this. Below is my code for your look up.

    try
    {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory( new File( "./") );
    int actionDialog = chooser.showSaveDialog(this);
    if ( actionDialog == JFileChooser.APPROVE_OPTION )
    {
    //File fileName = new File( chooser.getSelectedFile( ) + ".log" );
    File fileName = chooser.getSelectedFile();
    String temp = fileName.toString();
    String name = temp.substring(temp.lastIndexOf("\\") + 1, temp.lastIndexOf("."));
    //File file_name = new File(name);
    //System.out.println(name);
    if(fileName.exists())
    {
    int confirmDialog = JOptionPane.showConfirmDialog(this, "Replace existing file?");
    if (confirmDialog == JOptionPane.NO_OPTION)
    {
    chooser.showSaveDialog(this);

    }
    if(actionDialog == JOptionPane.YES_OPTION)
    {
    BufferedWriter outFile = new BufferedWriter( new FileWriter( name ) );
    outFile.write( getTxtArea_log().getText( ) ); //put in textfile
    outFile.flush( ); // redundant, done by close()
    outFile.close( );
    }

    //AttestDialog.getInstance( ).showErrorDialog(languageBundle.getString("LogFil eAlreadyExists"));
    }
    else
    {
    BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
    outFile.write( getTxtArea_log().getText( ) ); //put in textfile
    outFile.flush( ); // redundant, done by close()
    outFile.close( );
    }

    }
    }
    Looking forward for your reply. We should use our customized code to meet my requirement i think so. I should regain the save dialog if cancel button is clicked in the confirm dialog.

    Regards,
    Prabhu.

  8. #8
    kasi is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Post

    hi prabhurangan, try this code.I am using the same code in project.

    Java Code:
     try
                    	 {
                    	 JFileChooser chooser = new JFileChooser();
                    	 chooser.setCurrentDirectory( new File( "./") );
                    	 int actionDialog = chooser.showSaveDialog(this);
                    	 if ( actionDialog == JFileChooser.APPROVE_OPTION )
                    	 {
                    	 File fileName = new File( chooser.getSelectedFile( ) + ".log" );
                    	 if(fileName != null)
                    	 {
                    	 if(fileName.exists())
                    	 {
                    	 actionDialog = JOptionPane.showConfirmDialog(this, "Replace existing file?");
    
                    	 while (actionDialog == JOptionPane.NO_OPTION)
                    	 {
                    	 actionDialog=chooser.showSaveDialog(this);
                    	 if (actionDialog == JFileChooser.APPROVE_OPTION)
                    	  {
                    	         fileName = new File( chooser.getSelectedFile( ) + ".log" );
                    	          if(fileName.exists())
                    	          {
                    	        	  actionDialog = JOptionPane.showConfirmDialog(this, "Replace existing file?");
                    	                             	 					
                    	            }
                    	   }
    
                    	 }
                    	 if(actionDialog == JOptionPane.YES_OPTION)
                    	 {
                    	 BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
                    	 outFile.write( "hhhhh" ); //put in textfile
                    	 outFile.flush( ); // redundant, done by close()
                    	 outFile.close( );
                    	 }
                    	 return;
                    	 //AttestDialog.getInstance( ).showErrorDialog(languageBundle.getString("LogFil eAlreadyExists"));
                    	 }
    
                    	 BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
                    	 outFile.write( "hhhh" ); //put in textfile
                    	 outFile.flush( ); // redundant, done by close()
                    	 outFile.close( );
    
                    	 }
                    	 }
                    	 }
    Last edited by Eranga; 07-09-2010 at 04:08 AM. Reason: code tags added

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by prabhurangan View Post
    Hi Eranga,

    Please i need your help in this, issue too.
    I could not fix this. Below is my code for your look up.



    Looking forward for your reply. We should use our customized code to meet my requirement i think so. I should regain the save dialog if cancel button is clicked in the confirm dialog.

    Regards,
    Prabhu.
    Sorry for the late reply. Can you please explain a bit on where you stuck. I've look into your code, and nothing found major, except this.

    Java Code:
    outFile.write(getTxtArea_log().getText()); //put in textfile
    Is this return a string?

  10. #10
    tblasche is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Re: [SOLVED] jfilechooser for saving a file....?

    Hi all!

    Maybe this post is a little late, but for everybody facing the same problem here is a good solution (at least in my opinion):

    Java Code:
    // declare JFileChooser
    JFileChooser fileChooser = new JFileChooser();
    
    // let the user choose the destination file
    if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
        // indicates whether the user still wants to export the settings
        boolean doExport = true;
    
        // indicates whether to override an already existing file
        boolean overrideExistingFile = false;
    
        // get destination file
        File destinationFile = new File(fileChooser.getSelectedFile().getAbsolutePath());
    
        // check if file already exists
        while (doExport && destinationFile.exists() && !overrideExistingFile) {
            // let the user decide whether to override the existing file
            overrideExistingFile = (JOptionPane.showConfirmDialog(this, "Replace file?", "Export Settings", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION);
    
            // let the user choose another file if the existing file shall not be overridden
            if (!overrideExistingFile) {
                if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
                    // get new destination file
                    destinationFile = new File(fileChooser.getSelectedFile().getAbsolutePath());
                } else {
                    // seems like the user does not want to export the settings any longer
                    doExport = false;
                }
            }
        }
    
        // perform the actual export
        if (doExport) {
            // the code to write the file goes here...
        }
    }
    Regards,
    tblasche

  11. #11
    zeniah is offline Member
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Talking Re: [SOLVED] jfilechooser for saving a file....?

    Quote Originally Posted by tblasche View Post
    Hi all!

    Maybe this post is a little late, but for everybody facing the same problem here is a good solution (at least in my opinion):

    Java Code:
    // declare JFileChooser
    JFileChooser fileChooser = new JFileChooser();
    
    // let the user choose the destination file
    if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
        // indicates whether the user still wants to export the settings
        boolean doExport = true;
    
        // indicates whether to override an already existing file
        boolean overrideExistingFile = false;
    
        // get destination file
        File destinationFile = new File(fileChooser.getSelectedFile().getAbsolutePath());
    
        // check if file already exists
        while (doExport && destinationFile.exists() && !overrideExistingFile) {
            // let the user decide whether to override the existing file
            overrideExistingFile = (JOptionPane.showConfirmDialog(this, "Replace file?", "Export Settings", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION);
    
            // let the user choose another file if the existing file shall not be overridden
            if (!overrideExistingFile) {
                if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
                    // get new destination file
                    destinationFile = new File(fileChooser.getSelectedFile().getAbsolutePath());
                } else {
                    // seems like the user does not want to export the settings any longer
                    doExport = false;
                }
            }
        }
    
        // perform the actual export
        if (doExport) {
            // the code to write the file goes here...
        }
    }
    Regards,
    tblasche
    Thank you for posting this! It helped me out a lot.

Similar Threads

  1. JFileChooser example (selecting a directory)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 12:02 PM
  2. Localize JFileChooser
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 11:54 AM
  3. Saving data in an XML file
    By Thez in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 09:24 PM
  4. how to use JFileChooser
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •