1

Question might confuse you but read below for clarifications...

I am making a simple console project to get a extension of a file by inputting name from user. I am using solution described here:

How do I get the file extension of a file in Java?

It solve my 90% problem I have split file name on the basis of "." and "/".

But it will not run for input "a.out" or some otjer examples like this it will give extension as ".out" actually being extension less

So is there any solution for thia case????

please help me

Sorry for my english

Community
  • 1
  • 1

1 Answers1

2

I assume you are looking for getting the extension of file without the .(Dot) and return empty string if there is no extension. The following code may provide the desired outcome.

public static void main(String[] args){
    System.out.println(retrieveFileExtension("input.txt"));
}

private static String retrieveFileExtension(String fileName) {
    try {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    } catch (Exception e) {
        return "";
    }
}
Sajeev
  • 818
  • 7
  • 23