论坛首页 Java企业应用论坛

最简获取文件扩展名

浏览 9244 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (10)
作者 正文
   发表时间:2011-05-12  
pollyduan 写道
yangyi 写道
性能和可读性俱不佳

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

痛快是痛快了,但是当心异常,哈哈,遇上系统文件hosts就崩了
0 请登录后投票
   发表时间:2011-05-12   最后修改: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);
  }
0 请登录后投票
   发表时间: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);
    }
0 请登录后投票
   发表时间:2011-05-12  
indexOfExtension 方法呢,老兄?
0 请登录后投票
   发表时间:2011-05-12  
你们老是喜欢喷别人嘛?
0 请登录后投票
   发表时间:2011-05-12  
貌似可以使用String的 endWith不是来个更简洁?
0 请登录后投票
   发表时间:2011-05-12  
田智伟 写道
貌似可以使用String的 endWith不是来个更简洁?

for example?
0 请登录后投票
   发表时间:2011-05-12  
org.apache.commons.lang.StringUtils.substringAfterLast("index.htm", ".") ;
0 请登录后投票
   发表时间:2011-05-18  
freish 写道
获取扩展名有啥意义?



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

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

如果真的想获取文件扩展名,建议通过文件头里面的标识来获取,可能正确性会高一点。
0 请登录后投票
   发表时间:2011-05-18  
yangyi 写道
性能和可读性俱不佳

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

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

好用就行了。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics