为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Java中不带扩展名的文件名?

如何在Java中不带扩展名的文件名?

月关宝盒 2019-11-22 16:05:13
谁能告诉我如何获取不带扩展名的文件名?例:fileNameWithExt = "test.xml";fileNameWithOutExt = "test";
查看完整描述

3 回答

?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

如果像我一样,如果您希望在某些可能考虑了所有特殊情况的地方使用一些库代码,例如,如果在路径中输入null或点而不在文件名中输入时会发生什么,则可以使用以下代码:


import org.apache.commons.io.FilenameUtils;

String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);


查看完整回答
反对 回复 2019-11-22
?
郎朗坤

TA贡献1921条经验 获得超9个赞

请参阅以下测试程序:


public class javatemp {

    static String stripExtension (String str) {

        // Handle null case specially.


        if (str == null) return null;


        // Get position of last '.'.


        int pos = str.lastIndexOf(".");


        // If there wasn't any '.' just return the string as is.


        if (pos == -1) return str;


        // Otherwise return the string, up to the dot.


        return str.substring(0, pos);

    }


    public static void main(String[] args) {

        System.out.println ("test.xml   -> " + stripExtension ("test.xml"));

        System.out.println ("test.2.xml -> " + stripExtension ("test.2.xml"));

        System.out.println ("test       -> " + stripExtension ("test"));

        System.out.println ("test.      -> " + stripExtension ("test."));

    }

}

输出:


test.xml   -> test

test.2.xml -> test.2

test       -> test

test.      -> test


查看完整回答
反对 回复 2019-11-22
  • 3 回答
  • 0 关注
  • 1777 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信