谁能告诉我如何在没有扩展名的情况下获取文件名? 例子:

fileNameWithExt = "test.xml";
fileNameWithOutExt = "test";

当前回答

流利的方式:

public static String fileNameWithOutExt (String fileName) {
    return Optional.of(fileName.lastIndexOf(".")).filter(i-> i >= 0)
            .filter(i-> i > fileName.lastIndexOf(File.separator))
            .map(i-> fileName.substring(0, i)).orElse(fileName);
}

其他回答

流利的方式:

public static String fileNameWithOutExt (String fileName) {
    return Optional.of(fileName.lastIndexOf(".")).filter(i-> i >= 0)
            .filter(i-> i > fileName.lastIndexOf(File.separator))
            .map(i-> fileName.substring(0, i)).orElse(fileName);
}

请看下面的测试程序:

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

仅限文件名,其中还包括完整路径。不需要外部库,正则表达式等等

    public class MyClass {
    public static void main(String args[]) {
  
  
      String file  = "some/long/directory/blah.x.y.z.m.xml";

      System.out.println(file.substring(file.lastIndexOf("/") + 1, file.lastIndexOf(".")));
     //outputs blah.x.y.z.m
    }

}

虽然我是重用库的忠实信徒,但是org.apache.commons.io JAR有174KB,这对于一个移动应用程序来说非常大。

如果您下载源代码并查看它们的FilenameUtils类,您可以看到有许多额外的实用程序,并且它确实可以处理Windows和Unix路径,这些都很可爱。

然而,如果你只是想要几个静态实用程序方法用于Unix风格的路径(带“/”分隔符),你可能会发现下面的代码很有用。

removeExtension方法保留路径的其余部分和文件名。还有一个类似的getExtension。

/**
 * Remove the file extension from a filename, that may include a path.
 * 
 * e.g. /path/to/myfile.jpg -> /path/to/myfile 
 */
public static String removeExtension(String filename) {
    if (filename == null) {
        return null;
    }

    int index = indexOfExtension(filename);

    if (index == -1) {
        return filename;
    } else {
        return filename.substring(0, index);
    }
}

/**
 * Return the file extension from a filename, including the "."
 * 
 * e.g. /path/to/myfile.jpg -> .jpg
 */
public static String getExtension(String filename) {
    if (filename == null) {
        return null;
    }

    int index = indexOfExtension(filename);

    if (index == -1) {
        return filename;
    } else {
        return filename.substring(index);
    }
}

private static final char EXTENSION_SEPARATOR = '.';
private static final char DIRECTORY_SEPARATOR = '/';

public static int indexOfExtension(String filename) {

    if (filename == null) {
        return -1;
    }

    // Check that no directory separator appears after the 
    // EXTENSION_SEPARATOR
    int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);

    int lastDirSeparator = filename.lastIndexOf(DIRECTORY_SEPARATOR);

    if (lastDirSeparator > extensionPos) {
        LogIt.w(FileSystemUtil.class, "A directory separator appears after the file extension, assuming there is no file extension");
        return -1;
    }

    return extensionPos;
}

com.google.common.io.Files

档案getNameWithoutExtension sourceFile。getName()。

能胜任一份工作吗