`
helin
  • 浏览: 148291 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

最简获取文件扩展名

    博客分类:
  • java
 
阅读更多
写道
fileName.replaceAll(".*\\.", "")
分享到:
评论
21 楼 greatghoul 2011-05-18  
感觉iteye里面的越来越多人喜欢扯蛋。。。
20 楼 greatghoul 2011-05-18  
duronshi 写道
freish 写道
获取扩展名有啥意义?



赞同,楼主这样获取文件扩展名有何意义?

如果一个exe文件,我将文件后缀改成jpg,那按楼主的方法获取到的是什么?exe or jpg?

如果真的想获取文件扩展名,建议通过文件头里面的标识来获取,可能正确性会高一点。


文件头里的标识不见得就是文件的真实扩展名,这种方法很不准确吧。
19 楼 greatghoul 2011-05-18  
yangyi 写道
性能和可读性俱不佳

觉得这方法挺不错,也没有觉得可读性有什么差的,退一步讲,即便差,加个注释就好了。

要说性能吧,那要看什么场景了,脱离的应用场景空谈性能也没有什么意思。

好用就行了。
18 楼 duronshi 2011-05-18  
freish 写道
获取扩展名有啥意义?



赞同,楼主这样获取文件扩展名有何意义?

如果一个exe文件,我将文件后缀改成jpg,那按楼主的方法获取到的是什么?exe or jpg?

如果真的想获取文件扩展名,建议通过文件头里面的标识来获取,可能正确性会高一点。
17 楼 xiaobao0501 2011-05-12  
org.apache.commons.lang.StringUtils.substringAfterLast("index.htm", ".") ;
16 楼 luciferdevil 2011-05-12  
田智伟 写道
貌似可以使用String的 endWith不是来个更简洁?

for example?
15 楼 田智伟 2011-05-12  
貌似可以使用String的 endWith不是来个更简洁?
14 楼 zhkchi 2011-05-12  
你们老是喜欢喷别人嘛?
13 楼 lyy3323 2011-05-12  
indexOfExtension 方法呢,老兄?
12 楼 guoqingcun 2011-05-12  
    /**
     * Gets the extension of a filename.获取文件名
     * <p>
     * This method returns the textual part of the filename after the last dot.
     * There must be no directory separator after the dot.
     * 文件名例子:
     * foo.txt      --> "txt"
     * a/b/c.jpg    --> "jpg"
     * a/b.txt/c    --> ""
     * a/b/c        --> ""
     * <p>
     * The output will be the same irrespective of the machine that the code is running on.
     *
     * @param filename the filename to retrieve the extension of.
     * @return the extension of the file or an empty string if none exists or <code>null</code>
     * if the filename is <code>null</code>.
     */
   
public static String getExtension(String filename) {
        if (filename == null) {
            return null;
        }
        int index = indexOfExtension(filename);
        if (index == -1) {
            return "";
        } else {
            return filename.substring(index + 1);
        }
    }

----------------------------------------------------------------------------------
    /**
     * Returns the index of the last extension separator character, which is a dot.最后"."位置
     * <p>
     * This method also checks that there is no directory separator after the last dot.
     * To do this it uses {@link #indexOfLastSeparator(String)} which will
     * handle a file in either Unix or Windows format.
     * <p>
     * The output will be the same irrespective of the machine that the code is running on.
     *
     * @param filename  the filename to find the last path separator in, null returns -1
     * @return the index of the last separator character, or -1 if there
     * is no such character
     */
   public static int indexOfExtension(String filename) {
        if (filename == null) {
            return -1;
        }
        int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
        int lastSeparator = indexOfLastSeparator(filename);
        return (lastSeparator > extensionPos ? -1 : extensionPos);
    }

------------------------------------------------------------------------------------------
    /**
     * Returns the index of the last directory separator character.最后目录分割符位置(unix or window)
     * <p>
     * This method will handle a file in either Unix or Windows format.
     * The position of the last forward or backslash is returned.
     * <p>
     * The output will be the same irrespective of the machine that the code is running on.
     *
     * @param filename  the filename to find the last path separator in, null returns -1
     * @return the index of the last separator character, or -1 if there
     * is no such character
     */
  
 public static int indexOfLastSeparator(String filename) {
        if (filename == null) {
            return -1;
        }
        int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR);
        int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
        return Math.max(lastUnixPos, lastWindowsPos);
    }
11 楼 zhang34082 2011-05-12  
刚翻了下开源包commons-io-1.4.jar中FilenameUtils类中取扩展名的实现
public static String getExtension(String filename)
  {
    if (filename == null) {
      return null;
    }
    int index = indexOfExtension(filename);
    if (index == -1) {
      return "";
    }
    return filename.substring(index + 1);
  }
10 楼 003 2011-05-12  
pollyduan 写道
yangyi 写道
性能和可读性俱不佳

还不如fileName.substring(fileName.lastIndexOf(".")+1)来的痛快。

痛快是痛快了,但是当心异常,哈哈,遇上系统文件hosts就崩了
9 楼 nakupanda 2011-05-12  
怎么写直观性能又好呢 ?
8 楼 Reset 2011-05-12  
楼主刚学会正则 写的一个“Hello, World”鉴定完毕
7 楼 freish 2011-05-12  
获取扩展名有啥意义?
6 楼 pollyduan 2011-05-12  
yangyi 写道
性能和可读性俱不佳

还不如fileName.substring(fileName.lastIndexOf(".")+1)来的痛快。
5 楼 NanguoCoffee 2011-05-12  
helin 写道
grandboy 写道
helin 写道
暂不追求速度。

不追求速度,可读性又不好,为什么要这么写呢?要是我们的员工写成这样,肯定被我骂的。


你会要求你的员工对这句话也加注释吗?



别人说的意思是不能这样获取后缀。
而你的回答却是以这行代码为前提。


4 楼 helin 2011-05-12  
grandboy 写道
helin 写道
暂不追求速度。

不追求速度,可读性又不好,为什么要这么写呢?要是我们的员工写成这样,肯定被我骂的。


你会要求你的员工对这句话也加注释吗?
3 楼 grandboy 2011-05-12  
helin 写道
暂不追求速度。

不追求速度,可读性又不好,为什么要这么写呢?要是我们的员工写成这样,肯定被我骂的。
2 楼 helin 2011-05-12  
暂不追求速度。

相关推荐

    VB简单获取文件扩展名.rar

    VB简单获取文件扩展名,用法很简单:选择要获得扩展名的文件,点击操作按钮,最后就显示文件的扩展名了,简要说一下原理:从文件名的长度到文件名的第一个字符作循环,如果当前的字符是".",设置变量Ext的值为i,...

    获取文件扩展名(文件名已知)

    这是一个简单的小程序,一直文件的全名,然后通过C语言编程获取它的扩展名

    php简单获取文件扩展名的方法

    主要介绍了php简单获取文件扩展名的方法,实例分析了php获取文件扩展名的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

    PHP获取文件扩展名的常用方法小结【五种方式】

    本文实例总结了PHP获取文件扩展名的常用方法。分享给大家供大家参考,具体如下: 这是我应聘实习时遇到的一道笔试题: 使用五种以上方式获取一个文件的扩展名。 要求:dir/upload.image.jpg,找出 .jpg 或者 jpg , ...

    Java零基础(JDK13)-文件扩展名的显示.md

    本文档介绍了如何使用Java来获取文件的扩展名。通过阅读本文档,您将学习以下内容: 使用Java的File类来表示文件路径 以字符串形式获取文件的名称和路径 使用字符串处理方法获取文件的扩展名 能学到什么 通过阅读本...

    PHP 文件扩展名 获取函数

    有时候我们需要获取文件的扩展名,分类文件等原因,下面是php的函数实例代码。

    python获取文件扩展名的方法

    主要介绍了python获取文件扩展名的方法,涉及Python针对文件路径的相关操作技巧,非常简单实用,需要的朋友可以参考下

    php 如何获取文件的后缀名

    本文给大家汇总了几种使用PHP实现获取文件的后缀名的方法,十分的简单实用,有需要的小伙伴可以参考下

    C#获取图片文件扩展名的方法

    下面我给各位朋友整理了一篇C# 获取图片文件扩展名的例子,这里方法都非常的简单,我们只用到了image.RawFormat.Guid就实现了,具体看代码 例子 代码如下:/// /// 根据图像获取图像的扩展名 /// /// ”image”&gt; /...

    VB NET 获取下载地址扩展名

    简单获取http://wt4.jb51.net:81/201206/tools/excel2003_jb51.rar此类下载地址返回的扩展名(ContentType值) 文件大小 也可获取各网盘无tools/excel2003_jb51.rar返回的扩展名 支持:百度网盘 360 等 名字带有中文的...

    Python使用filetype精确判断文件类型

    filetype.py Small and dependency free Python package to infer file type and MIME type checking the magic numbers signature of a file or buffer. This is a Python port from ...•提供文件扩展名和MIME类型

    C#获取图片的后缀名解析

    对于一张知道全路径的照片,如果其路径包含后缀名的话,要取得后缀名,只需要一行代码即可: 代码如下:var ext = System.IO.Path.GetExtension(“C:\\soar.jpg”);可是,如果这个文件的文件名不包含后缀怎么办? 在...

    全功能文件重命名(功能强大!)

    ■ 扩展名变更:包括智能分析文件获取扩展名等高级扩展名变更在内的各种扩展名变更功能 ■ 特定文件文件名变更:包括提取标签给音乐文件批量更名、提取EXIF和IPTC信息给图片文件批量更名、提取多媒体(视频、音频、...

    signature:用于获取事实文件格式的通用库

    签名 English | 背景 在网络应用程序中,我们接受用户上传的文件, 并且很多时候需要知道用户上传的文件类型。...获取文件扩展名字符串的MIME TYPE var extension = " .jpg " ; var result = extension .

    file:Yii2扩展名,允许像使用简单属性(WIP)一样处理文件

    Yii2文件管理器Yii2扩展名,允许与使用简单属性一样处理文件。好处像其他任何简单属性一样使用文件。 存储逻辑是独立的,可以在不更改控制器或视图的情况下进行更改。 格式化程序逻辑是分开的。 您可以为每个文件...

    ASP得到文件的大小类型最后修改时间

    把下面文件另存为mofei.asp文件,运行即可,要FSO支持. &lt;&#37;Function fsofiledatemofei(sfile)'通过FSO得到文件的时间,类型,大小;sfile是文件名'制作:默飞'QQ:33224360'HOME: http://www.8vb.cnsfilere=...

    数据库文件加密(SDS)介绍

    SDS(SQL Data Security)是一...当服务器被盗时,可以是服务器电脑在本公司环境外部无法正常开机,卸载硬盘更换电脑依然无法正常开机,即数据库文件不会被外人获取。 SDS目前支持SQL2000、SQL2005、SQL2008、SQL2012。

    使用LINQ技术获取文件详细信息

    一、源码描述 这是一款采用Linq技术实现了获取文件详细信息的小程序,功能比较简单,对于初学Linq的朋友们来说,还是比较有价值的,有需要的朋友可以下载学习一下。二、功能介绍 该源码主要实现了通过选中文件夹,...

    vb源码 删除文本文件重复行

    另外,里边用上了自编获取文件路径名和扩展名函数 发布前专门测试通过,有需要的朋友可以直接引用 另外说明:由于好用才拿出来共享,为什么说测试无误,因为我最近搞的小程序“文本文件合并”就用到了这段代码。有...

Global site tag (gtag.js) - Google Analytics