简体   繁体   中英

How to ensure case-sensitive resource is what I look for on a case-insensitive file system?

I have a program which needs to lookup resources using ClassLoader#getResource(String) . The program normally works fine but on a Windows machine I'm running into a problem. I broke it down to property of the file system being case-insensitive. Normally, several paths are tried to look for the correct file. On a case-insensitive system, however, it is possible the wrong file is matched . In my program, resource names are case-sensitive , so the returned file on a case-insensitive system can be wrong if it has different capitalization.

For example: I am looking for bar/Foo.class and get back a file bar/FOO.class. I need to recognize this case and reject it .

What is the best way to check if the returned URL fits my specified path in a case-sensitive manner?

I was thinking about comparing the specified path with the result of getPath of the URL. But nothing anywhere tells me if the resulting URL will use my given path or the real path. Thus, unless someone shows me documentation that this is different and specified, I cannot be sure this will always be the case.

The problem is the fact you're not using absolute paths to find a certain resource. Therefor, the answer to your question is simply you can't . To a case insensitive system, there can only be one file at the path "bar/foo.class" from any one context . The only way there can be multiple files at such a path is if they have different absolute paths (eg /a/bar/foo.class and /b/bar/foo.class), in which case they have a different context (=there are two different folders "bar" which both contain a file named "foo.class").

When searching all resources matching a non-absolute path (like bar/foo.class), there is absolutely no guarantee there will only be one file on the system that matches that path (matches meaning the file's absolute path contains the path you specified). Further more, if there are multiple matches, there is no guarantee that they will differ in capitalization, so using case sensitivity to verify which file is wrong is not a good approach. Moreover, to a case insensitive system both files have the same name but are just at a different absolute location (but the same relative location to a different context [read: folder]).

If possible, search for files using their absolute path. That way, only the file with identical capitalization will be matched on case sensitive systems and the one file (if there is one) at that absolute path will be matched on case insensitive systems. I hope this answer is more useful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM