|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-17-2008, 09:15 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 49
|
|
|
[SOLVED] File chooser selecting file from directory...?
Hi All,
While selected a file from FileChooser, file name along with its extension is retrieved, but for me i need only the filename;
For example:
if my file name is 'name.log' and if i select the file name from the directory, i should get only the file name 'name' alone instead, im getting the file name with its extension as 'name.log' and if i save it, it consider it as a new file name and saves it as 'name.log.log' as a new file and file already exist error is not thrown.
Looking forward for reply.
Regards,
Prabhu.
|
|

06-17-2008, 09:22 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
|
Find the index of '.' and get the name, as a substring of the file name.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-17-2008, 12:00 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 49
|
|
|
Hi Eranga,
Im java.io.File to get the file name from JfileChooser;
File fileName = chooser.getSelectedFile();
and how to do substring with this, if it is a string type then we can do substring.
Looking forward for your reply.
Regards,
Prabhu.
|
|

06-17-2008, 12:18 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
|
First of all getSelecteFile() gives the full file path.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-17-2008, 12:29 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 49
|
|
|
Yes you are right eranga, but this gives the file path with the extension of the file also; if i select the file name that is already exist; example 'test' is my file name and its extension is '.txt' and if i select the file 'test', from the save dialog file directory, it retrieves the file name with its extension, 'test.txt' and treats it as a new file and if i click save it is not throwing any error, it saves as 'test.txt.txt'.
Eranga i hope you could get me, of what im trying to explain you.
Looking forward for your reply.
Regards,
Prabhu.
|
|

06-17-2008, 12:35 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
Ya, I get you. fileName is an object reference of File right? So what you can do?
File fileName = chooser.getSelectedFile();
String temp = fileName.toString();
String name = temp.substring(temp.lastIndexOf("\\") + 1, temp.lastIndexOf("."));
System.out.println(name);
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-17-2008, 01:00 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 49
|
|
Thank you Eranga; this is what i need, but can you please go through my last post, again im facing that issue.
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"));
}
How to use this modified file name in our dialog, when a file name is selected.
Regards,
Prabhu.
|
|

06-17-2008, 01:21 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
|
You mean still you have that validating the existing file?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-17-2008, 01:29 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 49
|
|
|
Yes Eranga,
I need my save as dialog to perform, as it is in a wordpad document. I think im saying again and again the same grinded flour. Can you please check a word document and try to save it with existing file name. The same logic i need it in java swing, while saving a file.
Regards,
Prabhu.
|
|

06-17-2008, 01:50 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
Check the simple logic, just written this and you have to change according to your application.
private void workignWithChooser() {
try {
JFileChooser chooser = new JFileChooser(new File("./"));
boolean isValid = true;
do{
int chooserStat = chooser.showSaveDialog(this);
if(chooserStat == JFileChooser.APPROVE_OPTION) {
// Do the process
File userFile = chooser.getSelectedFile();
if(userFile.exists()) {
System.out.println("File exist");
int result = JOptionPane.showConfirmDialog(this,
"Replace existing file?");
if(result == JOptionPane.YES_OPTION) {
// Save and exit
isValid = false;
}
else if(result == JOptionPane.NO_OPTION) {
//any logic if you have
}
}
else {
System.out.println("file is not exsit");
}
}
else if(chooserStat == JFileChooser.CANCEL_OPTION) {
System.exit(0);
}
}while(isValid);
System.exit(0);
}
catch(Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-17-2008, 02:03 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 49
|
|
|
Thank you very much Eranga, I think you have given me the exact solution, let me check it and let you know.
Regards,
Prabhu.
|
|

06-17-2008, 02:33 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 49
|
|
|
Thank you very much Eranga. I got my solution, after making some modifications to your code. I will get back to you on further post.
Bye take care.
|
|

06-18-2008, 05:08 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
|
|
|
Nice to here that. if you have solve this question, don't forget to mark as SOLVED.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|