I have a file,sometimes with extension or with out extension. Using java how to find out whether a particular file having extension
Asked
Active
Viewed 640 times
-4
-
7Show us your effort. Voting to close as whatever. – djechlin Jun 20 '13 at 18:22
-
1do you know the file name? if the name has a `.` in it followed by some letters, it has an extension – Sam I am says Reinstate Monica Jun 20 '13 at 18:22
-
Letters or numbers. For Blah.123, the extension is .123 – Eric J. Jun 20 '13 at 18:23
-
http://stackoverflow.com/questions/4545937/java-splitting-the-filename-into-a-base-and-extension – Costi Ciudatu Jun 20 '13 at 18:24
1 Answers
2
Commons-io has a method that will get a file extension from a path or filename: FilenameUtil.getExtension for example
String result = FilenameUtils.getExtension( "foo.txt" );
Gives the result txt
. If the file has no extension you will get the empty string back. A simple isEmpty
check on the string will tell you if there is an extension. From the documentation:
When dealing with filenames you can hit problems when moving from a Windows based development machine to a Unix based production machine. This class [Commons-io] aims to help avoid those problems.
This method returns the textual part of the filename after the last dot. There must be no directory separator after the dot.

Community
- 1
- 1

wobblycogs
- 4,083
- 7
- 37
- 48
-
if you make this something other than a link-only answer, I'll undownvote – Sam I am says Reinstate Monica Jun 20 '13 at 18:24
-
@SamIam: I would generally agree with you, but in this case there's not much more to say than that. – Eric J. Jun 20 '13 at 18:25
-
1Thanks, for the up vote. It's a one line question that requires a one line answer. What more could I say? – wobblycogs Jun 20 '13 at 18:25
-
@wobblycogs well, if you don't want to add anything more(like an example or something) than that's your prerogative, but my downvote will remain if you don't – Sam I am says Reinstate Monica Jun 20 '13 at 18:26
-
-