一、先获取带扩展名的fileName
String filePath = "D:\\manage\\uploadFile\\2023-06-28283\\初测.xlsx";File file = new File(filePath);// 或者Path path = Paths.get(filePath);String fileName1 = file.getName();System.out.println(fileName1);Path fileNamePath = path.getFileName();String fileName2 = fileNamePath.toString();System.out.println(fileName2);
二、再通过截取的办法去掉扩展名
//使用String的substring()方法截取文件名字部分String fileNameWithExtension = file.getName();String fileName3 = fileNameWithExtension.substring(0, fileNameWithExtension.lastIndexOf("."));System.out.println(fileName3);//使用正则表达式去除文件扩展名部分:String fileName4 = fileNameWithExtension.replaceAll("\\.\\w+$", "");System.out.println(fileName4);
来源地址:https://blog.csdn.net/weixin_49171365/article/details/131437983