Results 1 to 10 of 10
- 06-17-2008, 07:04 AM #1
Member
- Join Date
- May 2008
- Posts
- 69
- Rep Power
- 0
[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
- 06-17-2008, 07:37 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You don't have validate the file is exist or not even after the no options is selected. Just think about the logic there.
- 06-17-2008, 07:42 AM #3
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")); }
- 06-17-2008, 07:51 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Now is the logic you have miss.
- 06-17-2008, 07:55 AM #5
Member
- Join Date
- May 2008
- Posts
- 69
- Rep Power
- 0
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.
- 06-17-2008, 11:10 AM #6
Member
- Join Date
- May 2008
- Posts
- 69
- Rep Power
- 0
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.
- 06-17-2008, 12:06 PM #7
Member
- Join Date
- May 2008
- Posts
- 69
- Rep Power
- 0
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.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( );
}
}
}
Regards,
Prabhu.
- 07-08-2010, 11:30 AM #8
Member
- Join Date
- Jul 2010
- Posts
- 1
- Rep Power
- 0
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
- 07-09-2010, 04:16 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 03-19-2012, 05:20 PM #10
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
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):
Regards,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... } }
tblasche
Similar Threads
-
JFileChooser example (selecting a directory)
By Java Tip in forum Java TipReplies: 1Last Post: 02-03-2009, 01:25 PM -
Localize JFileChooser
By Java Tip in forum Java TipReplies: 0Last Post: 03-14-2008, 11:54 AM -
Saving data in an XML file
By Thez in forum New To JavaReplies: 1Last Post: 12-08-2007, 09:24 PM -
how to use JFileChooser
By tommy in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks