Checking file extension[SOLVED]

(Got working code) - Returns wrong values

Page 1 of 1

4 Replies - 3258 Views - Last Post: 28 November 2008 - 10:37 AM Rate Topic: -----

#1 Ironwarrior  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 26-November 08

Checking file extension[SOLVED]

Posted 28 November 2008 - 03:11 AM

Here is my code that I am using to check a file extension:

String fileExtension = fileName.substring(fileName.lastIndexOf('.')+1,filename.length());



If its a txt file returns:

txt,userFileFiding=true]

Not sure whats happening, but thanks in advance

This post has been edited by Ironwarrior: 28 November 2008 - 09:46 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Checking file extension[SOLVED]

#2 abgorn  Icon User is offline

  • sudo apt-get install brain
  • member icon

Reputation: 30
  • View blog
  • Posts: 1,409
  • Joined: 05-June 08

Re: Checking file extension[SOLVED]

Posted 28 November 2008 - 05:14 AM

This gets the file extension:
String filename = new java.io.File("filename.txt").getName();
String ext = (filename.lastIndexOf(".")==-1)?"":filename.substring(filename.lastIndexOf(".")+1,filename.length());


Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,649
  • Joined: 20-September 08

Re: Checking file extension[SOLVED]

Posted 28 November 2008 - 06:27 AM

You can also do the following, which also copes with Unix hidden files (.XYZ)


String extension = filename.replaceAll("[^\\.].*\\.([^\\.]+)$","$1"));


Was This Post Helpful? 1
  • +
  • -

#4 Ironwarrior  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 26-November 08

Re: Checking file extension[SOLVED]

Posted 28 November 2008 - 09:45 AM

Just wanted to say thanks too you both, works well within my program.

Thanks,

Iron warrior
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,649
  • Joined: 20-September 08

Re: Checking file extension[SOLVED]

Posted 28 November 2008 - 10:37 AM

:-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1