user3852473
user3852473

Reputation: 53

Check File Extension in Java/Android

How can I check the FileExtension of a URL which is sent from server in jsonResponce.

I want to pass a method if my URL has ".flv" in the end.

for example

http://derana.lk/apps/apple/tvderana/iframe_video.php?file=SriGauthamaSambuddha_35_19673.flv

Above link has ".flv" in the end.So i know that is a flv video. i want to know a method to check if my url has .flv in the end and if it has pass a method.

Upvotes: 2

Views: 5968

Answers (1)

Umer Kiani
Umer Kiani

Reputation: 3380

In this case, use FilenameUtils.getExtension from Apache Commons IO http://commons.apache.org/proper/commons-io/javadocs/api-1.4/org/apache/commons/io/FilenameUtils.html#getExtension%28java.lang.String%29

Here is an example of use:

String ext = FilenameUtils.getExtension("YOUR STRING LINK HERE.flv");

if(ext.equalsIgnoreCase("flv"){
//IT IS FLV

 yourMethodToPlay();
}

or

String extension = "";

int i = fileName.lastIndexOf('.');
if (i > 0) {
extension = fileName.substring(i+1);
}

Assuming that you're dealing with simple Windows-like file names, not something like archive.tar.gz.

Btw, for the case that a directory may have a '.', but the filename itself doesn't (like /path/to.a/file), you can do

String extension = "";

int i = fileName.lastIndexOf('.');
int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));

if (i > p) {
extension = fileName.substring(i+1);
}

However the Simplest way is to use FilenameUtils.getExtension Method

Upvotes: 14

Related Questions