Is there a new Java 8 way of retrieving the file extension?

后端 未结 5 1932
闹比i
闹比i 2021-02-07 06:21

What I did up until now is following:

String fileName = \"file.date.txt\";
String ext = fileName.substring(fileName.lastIndexOf(\'.\') + 1);

System.out.printf(\         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 07:00

    No there is no more efficient/convenient way in JDK, but many libraries give you ready methods for this, like Guava: Files.getFileExtension(fileName) which wraps your code in single method (with additional validation).

提交回复
热议问题