Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JfileChooser file selection.........?

 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Ranchers,
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.

 
Jim Jacob
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you do something like this:
Get the selected file name with extension using getSelectedFile() from th e FileChooser, then extract the file name without the etension from it, before attempting to save.
Something like this:
filename.substring(0,filename.lastIndexOf('.'))
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Jim,
Im using a I/O file 'java.io.File', example

File fileName = chooser.getSelectedFile();
In this case how to extract it, if it is a string we can do as what you said;
Looking forward for your reply.

Regards,
Prabhu.

 
Jim Jacob
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can still be done, using getName() method of the java.io.File. If you do this, the you will have to re-write the code that saves the file, to use this modified file name.

Instead of doing all these, why dont you remove the code which is adding the ".log" extension to the file being saved?
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jim Jacob K",

Please check your private messages regarding an important announcement.

Thank you,

Rob
 
prabhu pandurangan
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ROB,
I did not receive any private message.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't remember asking YOU to check your private messages
My request was directed at Jim Jacob.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic