任何人都可以告诉我如何获得没有扩展名的文件名?例:
fileNameWithExt = "test.xml";
fileNameWithOutExt = "test";
Run Code Online (Sandbox Code Playgroud)
Ulf*_*ack 402
如果你像我一样,宁愿使用一些库代码,他们可能已经考虑过所有特殊情况,比如如果你在路径中传入null或点而不是在文件名中传递会发生什么,你可以使用以下命令:
import org.apache.commons.io.FilenameUtils;
String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);
Run Code Online (Sandbox Code Playgroud)
bri*_*gge 144
最简单的方法是使用正则表达式.
fileNameWithOutExt = "test.xml".replaceFirst("[.][^.]+$", "");
Run Code Online (Sandbox Code Playgroud)
上面的表达式将删除最后一个点后跟一个或多个字符.这是一个基本的单元测试.
public void testRegex() {
assertEquals("test", "test.xml".replaceFirst("[.][^.]+$", ""));
assertEquals("test.2", "test.2.xml".replaceFirst("[.][^.]+$", ""));
}
Run Code Online (Sandbox Code Playgroud)
pax*_*blo 53
请参阅以下测试程序:
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."));
}
}
Run Code Online (Sandbox Code Playgroud)
哪个输出:
test.xml -> test
test.2.xml -> test.2
test -> test
test. -> test
Run Code Online (Sandbox Code Playgroud)
Jon*_*nik 40
如果您的项目使用Guava(14.0或更新版本),您可以使用Files.getNameWithoutExtension()
.
(基本上与FilenameUtils.removeExtension()
Apache Commons IO 相同,正如最高投票的答案所暗示的那样.只是想指出Guava也这样做.我个人也不想在Commons上添加依赖 - 我觉得这是一个遗物 -正因为如此.)
Om.*_*Om. 39
这是我的偏好的合并列表顺序.
使用apache commons
import org.apache.commons.io.FilenameUtils;
String fileNameWithoutExt = FilenameUtils.getBaseName(fileName);
OR
String fileNameWithOutExt = FilenameUtils.removeExtension(fileName);
Run Code Online (Sandbox Code Playgroud)
使用Google Guava(如果你已经在使用它)
import com.google.common.io.Files;
String fileNameWithOutExt = Files.getNameWithoutExtension(fileName);
Run Code Online (Sandbox Code Playgroud)
或者使用Core Java
1)
String fileName = file.getName();
int pos = fileName.lastIndexOf(".");
if (pos > 0 && pos < (fileName.length() - 1)) { // If '.' is not the first or last character.
fileName = fileName.substring(0, pos);
}
Run Code Online (Sandbox Code Playgroud)
2)
if (fileName.indexOf(".") > 0) {
return fileName.substring(0, fileName.lastIndexOf("."));
} else {
return fileName;
}
Run Code Online (Sandbox Code Playgroud)
3)
private static final Pattern ext = Pattern.compile("(?<=.)\\.[^.]+$");
public static String getFileNameWithoutExtension(File file) {
return ext.matcher(file.getName()).replaceAll("");
}
Run Code Online (Sandbox Code Playgroud)
Liferay API
import com.liferay.portal.kernel.util.FileUtil;
String fileName = FileUtil.stripExtension(file.getName());
Run Code Online (Sandbox Code Playgroud)
对于 Kotlin 来说,现在很简单:
val fileNameStr = file.nameWithoutExtension
Run Code Online (Sandbox Code Playgroud)
以下是来自https://android.googlesource.com/platform/tools/tradefederation/+/master/src/com/android/tradefed/util/FileUtil.java的参考
/**
* Gets the base name, without extension, of given file name.
* <p/>
* e.g. getBaseName("file.txt") will return "file"
*
* @param fileName
* @return the base name
*/
public static String getBaseName(String fileName) {
int index = fileName.lastIndexOf('.');
if (index == -1) {
return fileName;
} else {
return fileName.substring(0, index);
}
}
Run Code Online (Sandbox Code Playgroud)
虽然我非常相信重用库,但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;
}
Run Code Online (Sandbox Code Playgroud)
如果您不想导入完整的apache.commons,我已经提取了相同的功能:
public class StringUtils {
public static String getBaseName(String filename) {
return removeExtension(getName(filename));
}
public static int indexOfLastSeparator(String filename) {
if(filename == null) {
return -1;
} else {
int lastUnixPos = filename.lastIndexOf(47);
int lastWindowsPos = filename.lastIndexOf(92);
return Math.max(lastUnixPos, lastWindowsPos);
}
}
public static String getName(String filename) {
if(filename == null) {
return null;
} else {
int index = indexOfLastSeparator(filename);
return filename.substring(index + 1);
}
}
public static String removeExtension(String filename) {
if(filename == null) {
return null;
} else {
int index = indexOfExtension(filename);
return index == -1?filename:filename.substring(0, index);
}
}
public static int indexOfExtension(String filename) {
if(filename == null) {
return -1;
} else {
int extensionPos = filename.lastIndexOf(46);
int lastSeparator = indexOfLastSeparator(filename);
return lastSeparator > extensionPos?-1:extensionPos;
}
}
}
Run Code Online (Sandbox Code Playgroud)