• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to get filename without extension  RSS feed

 
Anuj Joshi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Can you please tell me that how to remove file extension from the file path.
i.e I have a file stored in the path D:/test/aaa.zip and I want file name from this path
I have written a code from which I get a file name aaa.zip but i dont want the file name with extension..
i want only aaa and not aaa.zip...

so can you please tell me how i can get filename without extension...

thanks...
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that you have the filename in a String, you can use the indexOf and/or lastIndexOf methods together with the substring method.
 
Scott Bravo
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If your need is extensive then you can also override the getName() method and apply ulf's suggested functionality to the overrided method..
-Bravo
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Bravo wrote:
If your need is extensive then you can also override the getName() method and apply ulf's suggested functionality to the overrided method..
-Bravo

Ehm, please don't. How would you get the name with the extension that way?

The best way is using a static utility method; this also allows you to reuse the code.

As Ulf suggested, lastIndexOf('.') will help you. However, you should consider two cases:
- the last index is -1, meaning there is no dot in the name. There is no extension.
- the last index is 0, meaning the only dot is the first character of the name. This can be considered as either "there is no extension", or "there is only an extension". The choice is up to you, but most explorer type programs use the first choice.
 
Anuj Joshi
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys thanks
as you said, I have used filename.lastIndexOf('.') and using this I got the file name

thanks once again
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!