Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-17-2008, 09:15 AM
Member
 
Join Date: May 2008
Posts: 49
prabhurangan is on a distinguished road
[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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-17-2008, 09:22 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-17-2008, 12:00 PM
Member
 
Join Date: May 2008
Posts: 49
prabhurangan is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-17-2008, 12:18 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-17-2008, 12:29 PM
Member
 
Join Date: May 2008
Posts: 49
prabhurangan is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-17-2008, 12:35 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ya, I get you. fileName is an object reference of File right? So what you can do?

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-17-2008, 01:00 PM
Member
 
Join Date: May 2008
Posts: 49
prabhurangan is on a distinguished road
Thank you Eranga; this is what i need, but can you please go through my last post, again im facing that issue.


Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-17-2008, 01:21 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-17-2008, 01:29 PM
Member
 
Join Date: May 2008
Posts: 49
prabhurangan is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-17-2008, 01:50 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Check the simple logic, just written this and you have to change according to your application.

Code:
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 06-17-2008, 02:03 PM
Member
 
Join Date: May 2008
Posts: 49
prabhurangan is on a distinguished road
Thank you very much Eranga, I think you have given me the exact solution, let me check it and let you know.

Regards,
Prabhu.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 06-17-2008, 02:33 PM
Member
 
Join Date: May 2008
Posts: 49
prabhurangan is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 06-18-2008, 05:08 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to read a file directory willemjav Java Applets 2 04-17-2008 05:54 PM
How to get Current Directory through File Java Tip java.io 0 04-05-2008 11:14 AM
Using File Chooser shaungoater New To Java 0 03-20-2008 01:30 PM
JFileChooser example (selecting a directory) Java Tip Java Tips 0 03-14-2008 01:02 PM
How to Move a File to Another Directory Ada New To Java 1 05-26-2007 02:17 PM


All times are GMT +3. The time now is 07:19 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org