-4

I have a file,sometimes with extension or with out extension. Using java how to find out whether a particular file having extension

Jojo John
  • 410
  • 1
  • 5
  • 14

1 Answers1

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