Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

how to get filename without extension

 
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...
 
Rancher
Posts: 43011
76
  • 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.
 
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
 
Sheriff
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!