如何获取没有Java扩展名的文件名? [英] How to get the filename without the extension in Java?

查看:100
本文介绍了如何获取没有Java扩展名的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁能告诉我如何获取没有扩展名的文件名?
示例:

Can anyone tell me how to get the filename without the extension? Example:

fileNameWithExt = "test.xml";
fileNameWithOutExt = "test";


推荐答案

如果你像我一样宁愿使用某些图书馆他们可能已经考虑过所有特殊情况的代码,比如如果你传入 null 或路径中的点而不是文件名中的点会发生什么,你可以使用以下代码:

If you, like me, would rather use some library code where they probably have thought of all special cases, such as what happens if you pass in null or dots in the path but not in the filename, you can use the following:

import org.apache.commons.io.FilenameUtils;
String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);

这篇关于如何获取没有Java扩展名的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆