aspose file tools
The moose likes Java in General and the fly likes remove extension from file name Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "remove extension from file name" Watch "remove extension from file name" New topic
Author

remove extension from file name

Cathy Song
Ranch Hand

Joined: Jul 01, 2003
Posts: 92
Hi All,

I can get the name of a file using getName(), but I want to remove the extension

Should I just write a substring to extract a filename, or is there a method in the API to do so.

Name: file.txt
I want to remove the .txt to extract just "file"

This code does the job do for:

[ August 11, 2005: Message edited by: Cathy Song ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24039
    
  13

There are other ways to do it, but there's no "removeExtension" method. Your way is as good as any other.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You might want lastIndexOf. I have some files that include versions like

BogWater1.3.1.exe


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Sathya Srinivasan
Ranch Hand

Joined: Jan 29, 2002
Posts: 379
The simplest version would be



The assumption is that regardless of how mant dots you have in the file name, the extension is always after the last dot.


Cheers, Sathya Srinivasan - SCJP 1.2, SCWCD 1.2, SCMAD 1.0
Co-Author of Whizlabs SCMAD Certification Exam Simulator and SCMAD Exam Guide Book
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
So to extract the filename using the same approach as Sathya:
Cathy Song
Ranch Hand

Joined: Jul 01, 2003
Posts: 92
Thanks a lot!

-Cathy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: remove extension from file name
 
Similar Threads
NumberFormatException Error
Referencing to another object
Text File Parse - Code comments please
Substring
getParent inquiry