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

后端 未结 5 1935
闹比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 06:56

    Use FilenameUtils.getExtension from Apache Commons IO

    Example:

    You can provide full path name or only the file name.

    String myString1 = FilenameUtils.getExtension("helloworld.exe"); // returns "exe"
    String myString2 = FilenameUtils.getExtension("/home/abc/yey.xls"); // returns "xls"
    

    Hope this helps ..

提交回复
热议问题