我正在创建一个用户从中选择文件的应用程序:
FilePicker.PickFile(文件名)
其中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的”);
OUTPUT:
hello.txt的
解决方法:
当您使用Windows(保留大小写(FAT32 / NTFS / ..))时,可以使用file.getCanonicalFile().getName()来获取所选文件的规范名称.
当您使用Linux或Android并且想要根据不一定匹配大小写的文件名选择文件时,遍历文件目录(file.getParent())中的所有文件,并选择.equalsIgnoreCase文件名.或者见Case-insensitive File.equals on case-sensitive file system
标签:java,string,file,io
来源: https://codeday.me/bug/20190729/1575224.html