Java – gets the case sensitive name of the file, not case sensitive spelling
I am creating an application from which users can select files:
FilePicker. Pickfile (file name)
Where filename is a string
In this method, it will be converted to:
File = new file (file name);
There is nothing wrong with that Next, I do,
if(file.exists()){ System.out.println(file.getName()); } else{ System.out.println("Fail."); }
This is the beginning of the problem I want to get the name of the file, such as "hello. TXT", but if the filename is "hello. TXT", it will still pass file Exists() check, while file Getname() returns "hello. TXT", "not" hello. TXT " Txt ". Is there any way to return file. Getname() as a case sensitive version to" hello. TXT? " thank you!
An example:
HELLO. Txt is a real file
FilePicker. Pickfile ("hello. TXT");
OUTPUT:
hello. Txt
Solution
When you use windows (keep case (FAT32 / NTFS /...) You can use file getCanonicalFile(). Getname() to get the specification name of the selected file
When you use Linux or Android and want to select files based on file names that do not necessarily match case, traverse all files in the file directory (file. Getparent()) and select Equalsignorecase file name Or see case sensitive file equals on case-sensitive file system