可以使用以下方法获取文件后缀:

File file = new File("path/to/file.txt");
String fileName = file.getName();
if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
    String fileExtension = fileName.substring(fileName.lastIndexOf(".")+1);
    System.out.println(fileExtension);
}

这里先获取文件名,然后判断文件名中是否有“.”,并且不是第一个字符,如果有,则获取最后一个“.”后面的字符串,即为文件后缀。

java File获取文件后缀

原文地址: https://www.cveoy.top/t/topic/f4Mq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录