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

查看:53
本文介绍了如何在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天全站免登陆