gpt4 book ai didi

java - 获取文件名区分大小写,拼写不区分大小写

转载 作者:搜寻专家 更新时间:2023-11-01 01:56:05 25 4
gpt4 key购买 nike

我正在制作一个应用程序,用户可以从中选择一个文件:

FilePicker.PickFile(文件名)

其中 filename 是一个字符串。

在方法中,会转化为:

File file = new File(filename);

这并没有错。接下来,我做,

if(file.exists()){
System.out.println(file.getName());
}
else{
System.out.println("Fail.");
}

这就是问题的开始。我想获取文件名,比如“HELLO.txt”,但是如果 filename 是“hello.txt”,它仍然会通过 file.exists()检查,file.getName() 返回为“hello.txt”,而不是“HELLO.txt”。有没有办法将 file.getName() 作为区分大小写的版本返回为“HELLO.txt?”谢谢!

一个例子:

HELLO.txt 才是真正的文件

FilePicker.PickFile("hello.txt");

输出:

你好.txt

最佳答案

当您使用保留大小写的 Windows (FAT32/NTFS/..) 时,您可以使用 file.getCanonicalFile() .getName() 获取所选文件的规范名称。

当您使用 Linux 或 Android 并且您想要根据不一定匹配大小写的文件名选择文件时,遍历文件目录中的所有文件 (file.getParent() ),然后选择 .equalsIgnoreCase filename 的那个。或查看 Case-insensitive File.equals on case-sensitive file system

关于java - 获取文件名区分大小写,拼写不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8750013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com