Java: splitting the filename into a base and extension

后端 未结 8 1946
我寻月下人不归
我寻月下人不归 2020-11-27 12:25

Is there a better way to get file basename and extension than something like

File f = ...
String name = f.getName();
int dot = name.lastIndexOf(\'.\');
Strin         


        
8条回答
  •  天涯浪人
    2020-11-27 12:39

    What's wrong with your code? Wrapped in a neat utility method it's fine.

    What's more important is what to use as separator — the first or last dot. The first is bad for file names like "setup-2.5.1.exe", the last is bad for file names with multiple extensions like "mybundle.tar.gz".

提交回复
热议问题