栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

删除Java中的文件扩展名

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

删除Java中的文件扩展名

我将为此目的使用双参数版本的stab,

lastIndexOf
以删除一些特殊情况的检查代码,并希望使意图更具可读性。幸得贾斯汀“jinguy”尼尔森提供这种方法的基础:

public static String removeExtention(String filePath) {    // These first few lines the same as Justin's    File f = new File(filePath);    // if it's a directory, don't remove the extention    if (f.isDirectory()) return filePath;    String name = f.getName();    // Now we know it's a file - don't need to do any special hidden    // checking or contains() checking because of:    final int lastPeriodPos = name.lastIndexOf('.');    if (lastPeriodPos <= 0)    {        // No period after first character - return name as it was passed in        return filePath;    }    else    {        // Remove the last period and everything after it        File renamed = new File(f.getParent(), name.substring(0, lastPeriodPos));        return renamed.getPath();    }}

对我来说,这比特殊格式的隐藏文件和不包含点的文件更清晰。它也使我更清楚地了解您的规格要求;类似于“删除最后一个点及其后的所有内容,并假设该点存在并且不是文件名的第一个字符”。

请注意,此示例还隐含了字符串作为输入和输出。由于大多数抽象都需要

File
对象,因此如果这些对象也同时是输入和输出,则会稍微清楚一点。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/496815.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号