Remove filename extension in Java

后端 未结 10 2227
你的背包
你的背包 2020-12-09 08:48

(Without including any external libraries.)

What\'s the most efficient way to remove the extension of a filename in Java, without assuming anything of the f

相关标签:
10条回答
  • 2020-12-09 09:39

    Using common io from apache http://commons.apache.org/io/

    public static String removeExtension(String filename)

    FYI, the source code is here:

    http://commons.apache.org/proper/commons-io/javadocs/api-release/src-html/org/apache/commons/io/FilenameUtils.html#line.1025

    Arg, I've just tried something...

    System.out.println(FilenameUtils.getExtension(".polop")); // polop
    System.out.println(FilenameUtils.removeExtension(".polop")); // empty string
    

    So, this solution seems to be not very good... Even with common io, you'll have to play with removeExtension() getExtension() indexOfExtension()...

    0 讨论(0)
  • 2020-12-09 09:39

    This will take a file path and then return the new name of the file without the extension.

    public static String removeExtention(String filePath) {
        File f = new File(filePath);
        // if it's a directory, don't remove the extention
        if (fisDirectory()) return f.getName();
        String name = f.getName();
        // if it is a hidden file
        if (name.startsWith(".")) {
            // if there is no extn, do not rmove one...
            if (name.lastIndexOf('.') == name.indexOf('.')) return name;
        }
        // if there is no extention, don't do anything
        if (!name.contains(".") return name;
        // Otherwise, remove the last 'extension type thing'
        return name.substring(0, name.lastIndexOf('.'))
    }
    

    People should note that this was written on my netbook, in the tiny SO editor box. This code is not meant for production. It is only meant to server as a good first attempt example of how I would go about removing the extension from a filename.

    0 讨论(0)
  • 2020-12-09 09:44

    I'm going to have a stab at this that uses the two-arg version of lastIndexOf in order to remove some special-case checking code, and hopefully make the intention more readable. Credit goes to Justin 'jinguy' Nelson for providing the basis of this method:

    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();
        }
    }
    

    To me this is clearer than special-casing hidden files and files that don't contain a dot. It also reads clearer to what I understand your specification to be; something like "remove the last dot and everything following it, assuming it exists and is not the first character of the filename".

    Note that this example also implies Strings as inputs and outputs. Since most of the abstraction requires File objects, it would be marginally clearer if those were the inputs and outputs as well.

    0 讨论(0)
  • 2020-12-09 09:46

    Regex for these things are "fast" enough but not efficient if compared to the simplest method that can be thought: scan the string from the end and truncate it at the first dot (not inclusive). In Java you could use lastIndexOf and substring to take only the part you are interested in. The initial dot should be considered as a special case and if the last occurrence of "." is at the beginning, the whole string should be returned.

    0 讨论(0)
提交回复
热议问题